Page 1 of 1
CModel return array instead object
#1
Posted 29 November 2010 - 12:42 PM
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
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
#2
Posted 29 November 2010 - 12:50 PM
This, maybe:
If you want to debug your queries, you can use a handy thing called CA_Debug:
http://www.kevinkorb.com/post/26
$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
"Less noise - more signal"
#3
Posted 29 November 2010 - 01:04 PM
try something like this
$cb = Yii::app()->db->getCommandBuilder();
$c = new CDbCriteria();//your criteria
$result = $cb->createFindCommand('yourTableName',$c)->queryRow();
#4
Posted 29 November 2010 - 01:18 PM
$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.
#5
Posted 29 November 2010 - 01:48 PM
Thanks for answers,
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.
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.
#6
Posted 29 November 2010 - 02:02 PM
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.
Use 'select' to only fetch the fields you want, and 'findAll' to return an array.
Which is - by the way - an object.
"Less noise - more signal"
#7
Posted 29 November 2010 - 02:51 PM
Quote
is there any way to return simple array by calling find or findAll from model
no
Quote
getCommandBuilder i need to write table name and queries.
Yes, but only tableName, instead query you can use CDBCriteria
#8
Posted 29 November 2010 - 03:21 PM
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.
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.
#9
Posted 29 November 2010 - 03:41 PM
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.
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.
"Less noise - more signal"
#10
Posted 29 November 2010 - 04:27 PM
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
Share this topic:
Page 1 of 1

Help













