Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Better print_r</h3> <p>To get a better <code>print_r</code> output in yii, you can use the <a href="http://www.yiiframework.com/doc/api/1.1/CVarDumper" rel="noreferrer" title="http://www.yiiframework.com/doc/api/1.1/CVarDumper">CVarDumper</a> class' <code>dump()</code> or <code>dumpAsString()</code> methods. They also provide a parameter <code>$highlight</code> which will help you make sense of the output, by properly formatting the output, and adding indentation to it. Example:</p> <pre><code>CVarDumper::dump($variables,10,true); // 10 is the default depth, and passing true will enable highlighting </code></pre> <hr> <h3>Why and what structure?</h3> <p>As already mentioned in the other answers <a href="http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail" rel="noreferrer" title="http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAll-detail"><code>findAll()</code></a> returns an array of <strong>CActiveRecord objects</strong>, so <code>$variables</code> is an array of objects, and <code>$variables[0]</code> is the first Post object. Yii's CActiveRecord has a host of properties which are objects, for instance a CActiveRecordMetaData object, which in turn has a CDbTableSchema object (and you have it's subclass the CMysqlTableSchema, which means you are using mysql). The <code>print_r</code> is simply printing out these objects, which are nothing but properties of the main CActiveRecord object. In addition to these objects the <code>attributes</code> property(which is an array) of a CActiveRecord holds your actual attribute values, so somewhere in the output you'll also see an array like this:</p> <pre><code>[CActiveRecord:_attributes] =&gt; array ( 'attributeName' =&gt; 'attributeValue' 'anotherAttributeName' =&gt; 'anotherAttributeValue' 'someAttributeName' =&gt; 'someAttributeValue' ... ) </code></pre> <p>Those are your attribute values.</p> <hr> <h3>How to access?</h3> <p>To access a model's properties we can use both object property access, and associative array access(Probably because CActiveRecord's parent class CModel implements php's <a href="http://php.net/manual/en/class.arrayaccess.php" rel="noreferrer" title="http://php.net/manual/en/class.arrayaccess.php">ArrayAccess interface</a>). Example:</p> <pre><code>$variables[0]-&gt;attributeName; $variables[0]['attributeName']; </code></pre> <p>And since yii uses and overrides the __get php magic method we can do:</p> <pre><code>$variables[0]-&gt;attributeName; // instead of $variables[0]-&gt;attributes['attributeName']; </code></pre> <p>And of course you can iterate over the array of Post objects using <code>foreach()</code> as already shown in another answer here:</p> <pre><code>foreach($variables as $aPost){ echo $aPost-&gt;attributeName; echo $aPost['attributeName']; echo $aPost-&gt;attributes['attributeName']; } </code></pre> <p>To access relations, just use the relation name: </p> <pre><code>$variables[0]-&gt;relationName-&gt;attributeOfRelatedTable; $variables[0]['relationName']-&gt;attributeOfRelatedTable; $variables[0]['relationName']['attributeOfRelatedTable']; </code></pre> <p>If your relation is HAS_MANY, then of course the related models will also be returned as an array:</p> <pre><code>$variables[0]-&gt;relationName[0]-&gt;attributeOfRelatedTable; $variables[0]['relationName'][0]-&gt;attributeOfRelatedTable; $variables[0]['relationName'][0]['attributeOfRelatedTable']; $variables[0]-&gt;relationName[0]['attributeOfRelatedTable']; </code></pre> <p>And again you can iterate over the relations array incase of HAS_MANY relations.</p> <p>Edit: example for has_many iteration:</p> <pre><code>foreach($variables as $aPost) { // get each post one by one echo $aPost-&gt;someAttribute; // or $aPost['someAttribute'] foreach($aPost-&gt;relationName as $aComment) { // say we get each comment of each post // or could have done $aPost['relationName'] as $aComment echo $aComment-&gt;commentAttribute; // or $aComment['commentAttribute'] } } </code></pre>
    singulars
    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. 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.
 

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