CModel return array instead object
Posted 29 November 2010 - 12:42 PM
Is there any way to return array data from model class, for example:
I need just row title, so I don't need to have back all object which takes about 50 kb of memory, It's simply enough recieve a simple array:
'title' => 'MyTitle'
for one record and it takes about 1 kb data.
With model help it's very easy to navigate with your data, but it takes a lot of resources when returning an object instead of array.
I understand that object is good for dataprovider but if I want to echo my data in simple way I don't need to get an Object.
Posted 29 November 2010 - 12:50 PM
$someCriteria->select = "title"; SomeModel::model()->findAll($someCriteria);
If you want to debug your queries, you can use a handy thing called CA_Debug:
Posted 29 November 2010 - 01:48 PM
I know that with
return array - list of active records and active record is still an object. In this case i just need simple multidimensional array, not array of objects.
I know that with
I can return array but it's interessting is there any way to return simple array by calling find or findAll from model, because it's very convenient and using getCommandBuilder i need to write table name and queries.
I'm asking this because of php memory leaks when retrieving a lot of objects instead of arrays.
And I don't need a 90 % of object data, because I used this data in foreach cycle not in Data grid where you must to provide activedataprovider.
Posted 29 November 2010 - 03:21 PM
I know well about select criteria and findAll method.
Exactly, I dont need an object, instead I want an array
But ok, I have found in other blogs that is nothing to do, you can use query or get an objects list with find or findAll and than cache results if you want to reduce memory usage.
Posted 29 November 2010 - 03:41 PM
Depending on how you use it, that could be heavier than just let Yii do it's job.
If you select only the fields you're interested in then findAll should return a very light weight array of objects.
Do use the CA_Debug class to inspect the results.
Don't rule that option out until you've looked into it a bit more.