How Do I Retrieve Assoc. Array With Cdbcriteria

I have in my model Events the code generated method search(), which returns with $this->search()->data an array of Events objects. However I want to have an associative array of Events data for my jqGrid with CDbCriteria not


Yii::app()->db->createCommand()->Select('...')->From('...')->Where('...')

.

Any idea how to do that with:




public function search(){


$criteria=new CDbCriteria;

		

$criteria->compare('id',$this->id);

$criteria->compare('Date',$this->Date,true);




}



"search" method returns a CActiveDataProvider, which will give you back an array of CAcitveRecord instances when you call CActiveDataProvider::getData() method. You can not get an associative array directly using CActiveDataProvider.

Try using CDbCommandBuilder with which you can use CDbCriteria for constructing SQLs.




$criteria = new CDbCriteria;

$criteria->compare('id',$this->id);

$criteria->compare('Date',$this->Date,true);

...

$commandBuilder = Yii::app()->db->commandBuilder;

$command = $commnandBuilder->createFindCommand($criteria);

$records = $command->queryAll();



See also CDbConnection::commandBuilder

Thanks for the great solution.

As I dig deep in the code with xdebug I found another solution that has a similar approach asking the model.


return $this->getCommandBuilder()->createFindCommand($this->getTableSchema(), $criteria)->queryAll();

Now the model extends from CActiveRecord, which makes sense why it has the method getCommandBuilder().

However I am puzzled why your approach works. I’m just surprised why


Yii::app()->db

a CDbConnection object has the property commandBuilder? Am I too anal?