Yii Framework Forum: CModel return array instead object - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CModel return array instead object Rate Topic: -----

#1 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

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
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 29 November 2010 - 12:50 PM

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
"Less noise - more signal"
0

#3 User is offline   rudenich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 30-April 10
  • Location:Ukraine-Kramatorsk

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();

0

#4 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

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.
0

#5 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

Posted 29 November 2010 - 01:48 PM

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.
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

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. ;)
"Less noise - more signal"
0

#7 User is offline   rudenich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 30-April 10
  • Location:Ukraine-Kramatorsk

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
0

#8 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

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.
0

#9 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

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.
"Less noise - more signal"
0

#10 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

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 ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users