CModel return array instead object

Hi,

Is there any way to return array data from model class, for example:

SomeModel::model()->find($someCriteria);

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:

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.

Thanks

This, maybe:


$someCriteria->select = "title";

SomeModel::model()->findAll($someCriteria);



If you want to debug your queries, you can use a handy thing called CA_Debug:

http://www.kevinkorb.com/post/26

try something like this




$cb = Yii::app()->db->getCommandBuilder();

$c = new CDbCriteria();//your criteria

$result = $cb->createFindCommand('yourTableName',$c)->queryRow();






$C = new CDbCriteria;

$C->select = "title";

$data = somemodel::model()->findAll($C);



returns array of data.




$C = new CDbCriteria;

$C->select = "title";

$data = somemodel::model()->find($C);



returns first data of column.

Thanks for answers,

I know that with




somemodel::model()->findAll



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




[color=#1C2837][size=2][color=#000000]getCommandBuilder[/color][/size][/color]



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.

I already told you how:

Use ‘select’ to only fetch the fields you want, and ‘findAll’ to return an array.

Which is - by the way - an object. ;)

no

Yes, but only tableName, instead query you can use CDBCriteria

thanks,

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.

An array is an object, God dammit. :)

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.

I understand, I’ll use active record, just reduse memory by selecting not all fields, using cache. I always can use CHtml::listData for an array, but any way thanks for answers, I got it ;)