Yii Framework Forum: Create a CDbCommand from a CDbCriteria - Yii Framework Forum

Jump to content

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

Create a CDbCommand from a CDbCriteria Get the low level benefits of a Command when all you have is Criteria

#1 User is offline   mendo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-May 12

Posted 17 May 2012 - 06:24 PM

If you have a CDbCriteria and want to create a CDbCommand out of it, use the following example (assuming a model class called Post):

$tableSchema = Post::model()->getTableSchema();
$command = Post::model()->getCommandBuilder()->createFindCommand($tableSchema, $yourCriteriaHere);


Inside a Post instance just say $this instead of Post::model()

The reason to bother with this is that CDbCommand offers some retrieval options that don't require creation of any model objects. I had a very complex Criteria that I didn't want to rewrite as a Command or SQL and I only wanted to get the ids of ~1000 models, rather than create 1000 models and loop. You can get an array of ids like this:

$command->select('id')->queryColumn();


Doing this brought the memory footprint of my request from 40mb+ to around 19mb. I hope this helps save you some time!


John M
------------------
www.x2engine.com
The New Open Source CRM Alternative
1

#2 User is offline   Pilsbier1 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 03-July 13

Posted 03 July 2013 - 09:40 AM

Thanks, that was, what i'm searching for ;-)
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