Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning object in Yii Component
    primarykey
    data
    text
    <p>This is hard to explain for me but I'll do my best.</p> <p>I am starting to write a CApplicationComponent to get config params from DB.</p> <pre><code>&lt;?php class parameters extends CApplicationComponent { public $cacheAll = false; public $dbConnectionId = null; public $applications = array(); public function __get( $attr ) { if( $this-&gt;applications &amp;&amp; in_array($attr, $this-&gt;applications) ) { if( Yii::app()-&gt;$$this-&gt;applications[$attr]['id'] ) { //in work... } } } } </code></pre> <p><code>public $applications</code> would be an array like this:</p> <pre><code>$arrray = array ( 'productName' =&gt; array ( 'tableName'=&gt;'settings', 'propertyColumn'=&gt;'parameters', 'valueColumn'=&gt;'values', ), ); </code></pre> <p>Well... the fact is that I need, or mostly want, to get an attribute like this:</p> <pre><code>Yii::app()-&gt;parameters-&gt;productName-&gt;param1 </code></pre> <p>I know that Yii implements magic getter and setters for class properties, so I would access <code>$applications</code> this way:</p> <pre><code>Yii::app()-&gt;parameters-&gt;applications </code></pre> <p>So, I am now overriding the <code>__get</code> function, to see if the requested value is a key in the array $applications, or else I will delegate it to the Yii's defined one with <code>parent::__get($attr)</code>.</p> <p>If the requested value is a key in the $applications array, then I would have to create an object so I can use the <code>-&gt;</code> operator, and another magic setter/getter, but I really don't know how yet.</p> <p>IS there a better way of doing it? Can anyone point me in the right direction?</p> <p>Thanks a lot</p>
    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.
 

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