Yii Ar Returns Object...


I did started to use Yii Framework, and it is better that last framework that I used. I have only one thing that gets to me. When I get something with AR, he will return to me object and I want array/object with only attributes (in object there are many things that I don’t need in templates). If I select one item from database with AR, case is simple:

$posts = Post::model()->find($condition,$params)->attriutes;

But when I run find all i must use foreach to get all attributes. Is there a different way to get array or object with only attributes?

Also if I select item/items with relations in model, there is still that object with items that I don’t need.

Hello Zagroda,

As of the moment I can’t find any other way than to use foreach if you are finding it using AR, maybe you can get the attributes by:



find, findAll etc used from Yii for Object Oriented purpose.

So If you want specific values from database use Query Builder (use queryAll method)


I wanted to use AR, but if there is no other option… Thanks

AR is programming pattern that works with objects (it is object-based). There is no way to "work with AR but without objects", and frankly I do not see any reason to work without objects in templates.

if you want to work with arrays - you must use standard DAO functions.

Maybe I did not write it clearly. I want array or object but already with attributes. What AR is returning now it makes lines in templates too long in my opinion.

Plz give an example of what you want. Currently it’s not very clear.

PS. Maybe custom getters will do.

@ORey - I think he is not aware that he can access the attributes directly with $modelName->attributeName and because of that he loops through the $modelName->attributes array and gets the key/value from the array.

@twisted1919 - omg, I did access attributes like you did show “$modelName->attributeName” in models! I used it and forgot about it :confused: I’m ashamed now! :-[

Funny :) But hey, it happens to the best of us so don’t worry ;)