Create a CDbCommand from a CDbCriteria

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


The New Open Source CRM Alternative

Thanks, that was, what i’m searching for :wink: