Better print_r
To get the best print_r result in yii, you can use CVarDumper class' dump() or dumpAsString() . They also provide the $highlight option, which helps you understand the result, format the output correctly, and add padding to it. Example:
CVarDumper::dump($variables,10,true);
Why and what structure?
As mentioned in other answers, findAll() returns an array of CActiveRecord objects, so $variables is an array of objects, and $variables[0] is the first Post object. Yii CActiveRecord has many properties that are objects, for example a CActiveRecordMetaData object, which in turn has a CDbTableSchema object (and you have a subclass of CMysqlTableSchema, which means you are using mysql). print_r simply prints these objects, which are nothing more than the properties of the main CActiveRecord object. In addition to these objects, the attributes property (which is an array) of CActiveRecord contains your actual attribute values, so somewhere in the output you will also see such an array:
[CActiveRecord:_attributes] => array ( 'attributeName' => 'attributeValue' 'anotherAttributeName' => 'anotherAttributeValue' 'someAttributeName' => 'someAttributeValue' ... )
These are your attribute values.
How to get access?
To access model properties, we can use both access to object properties and access to associative arrays (possibly because the CActiveRecord CModel parent class implements the php ArrayAccess Interface ). Example:
$variables[0]->attributeName; $variables[0]['attributeName'];
And since yii uses and overrides the __get php magic method we can do:
$variables[0]->attributeName; // instead of $variables[0]->attributes['attributeName'];
And of course, you can iterate over an array of Post objects using foreach() , as already shown in the following answer:
foreach($variables as $aPost){ echo $aPost->attributeName; echo $aPost['attributeName']; echo $aPost->attributes['attributeName']; }
To access relationships, simply use the relationship name:
$variables[0]->relationName->attributeOfRelatedTable; $variables[0]['relationName']->attributeOfRelatedTable; $variables[0]['relationName']['attributeOfRelatedTable'];
If your relation is HAS_MANY, then, of course, related models will also be returned as an array:
$variables[0]->relationName[0]->attributeOfRelatedTable; $variables[0]['relationName'][0]->attributeOfRelatedTable; $variables[0]['relationName'][0]['attributeOfRelatedTable']; $variables[0]->relationName[0]['attributeOfRelatedTable'];
And again, you can iterate over an array of relationships, taking into account the HAS_MANY relationships.
Edit: example for has_many iteration:
foreach($variables as $aPost) { // get each post one by one echo $aPost->someAttribute; // or $aPost['someAttribute'] foreach($aPost->relationName as $aComment) { // say we get each comment of each post // or could have done $aPost['relationName'] as $aComment echo $aComment->commentAttribute; // or $aComment['commentAttribute'] } }