February 25, 2014, 10:08am
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.
February 25, 2014, 11:05am
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:
February 25, 2014, 11:25am
find, findAll etc used from Yii for Object Oriented purpose.
So If you want specific values from database use Query Builder (use queryAll method)
February 25, 2014, 11:41am
I wanted to use AR, but if there is no other option… Thanks
February 25, 2014, 1:17pm
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.
February 25, 2014, 1:40pm
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.
February 25, 2014, 3:34pm
@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.
February 25, 2014, 3:49pm
@twisted1919 - omg, I did access attributes like you did show “$modelName->attributeName” in models! I used it and forgot about it I’m ashamed now!
February 25, 2014, 3:59pm
But hey, it happens to the best of us so don’t worry