Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your model, add the following method:</p> <pre><code>/** * Uses the primary keys set on a new record to either create or update * a record with those keys to have the last_access value set to the same value * as the current unsaved model. * * Returns the model with the updated last_access. Success can be checked by * examining the isNewRecord property. * * IMPORTANT: This method does not modify the existing model. **/ public function updateRecord(){ $model = self::model()-&gt;findByPk(array('user_id'=&gt;$this-&gt;user_id,'category_id'=&gt;$this-&gt;category_id)); //model is new, so create a copy with the keys set if(null === $model){ //we don't use clone $this as it can leave off behaviors and events $model = new self; $model-&gt;user_id = $this-&gt;user_id; $model-&gt;category_id = $this-&gt;category_id; } //At this point we have a model ready for saving, //and don't care if it is new or not $model-&gt;last_access = $this-&gt;last_access; $model-&gt;save(false); return $model; } </code></pre> <p>The above is inspired by a more general method that I use a lot to do a create-or-find-if-already-exists process.</p> <p>Use the following code to execute this.</p> <pre><code>$userCategory = new UserCategory; $userCategory-&gt;user_id = 1; $userCategory-&gt;category_id = 15; echo $userCategory-&gt;isNewRecord; //always true $userCategory-&gt;last_access = Now(); $userCategory = $userCategory-&gt;updateRecord(); </code></pre> <p>Note that only the last line is different from your code. The fact that the instance of the model declared with <code>new UserCategory</code> is not altered is intended behavior.</p> <p>You can then verify in your code whether or not the model saved with the following:</p> <pre><code>if(!$userCategory-&gt;isNewRecord){ echo 'save succeeded'; } else{ echo 'save failed'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload