Note that there are some explanatory texts on larger screens.

plurals
  1. POYii component property not calling get, when using getAttributes
    text
    copied!<p>I have a Model with a <a href="http://www.yiiframework.com/doc/guide/1.1/en/basics.component#component-property" rel="nofollow">Component property</a>. This value should be pre-calculated every time, when I initialize/get this Model (there is a function that generates it). </p> <p>I placed this 'default value' into the getter of the property:</p> <pre><code>public function getCid(){ if ($this-&gt;_cid == null ){ $this-&gt;_cid = generateCid(); } return $this-&gt;_cid; } </code></pre> <p>When I call it, its get method is properly working:</p> <pre><code>$model-&gt;cid; //returns good value </code></pre> <p>But, when I try to get it with other parameters with <code>getAttributes()</code> function, it doesn't call it's get method.</p> <pre><code>$model-&gt;getAttributes(array('cid')); //just to try only this property. </code></pre> <p>I don't want to trick with arrays to get it's value independently with <code>$model-&gt;cid</code> and merge it into the <code>getAttributes</code> return array, because I will have lot more properties in my app and I am looking for a fast solution.</p> <p>So where should I move this generator, or what should I change to get this generated id easily?</p> <p><strong>Update:</strong></p> <p>I created a base ActiveRecord class, that is extending the <code>CActiveRecord</code> and I added the following function:</p> <pre><code>public function getAttributes($names=true){ $base = parent::getAttributes($names); foreach($base as $key =&gt; $value) if(!$this-&gt;hasAttribute($key)) $base[$key] = $this[$key]; return $base; } </code></pre> <p>This calls every added attribute's get method, so it will update it's content.</p>
 

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