user3638269
user3638269

Reputation: 37

Yii db command and CDbCriteria

I have two tables Products(id, name) and Views(id,count,time), and those two tables are not related to each other. This is my code:

$dbCommand = Yii::app()->db->createCommand("
SELECT P.`id`, P.`name`, V.`time` 
FROM `products` P, `views` V 
WHERE P.`type` = 2 
ORDER BY V.`time` DESC
");
$data = $dbCommand->queryAll();

It is working, but I want to convert this query to CDbCriteria syntax.

$cdb = new CDbCriteria();
$cdb->select = //???
$cdb->where = //???
$cdb->order = //???

How can I do this? Can somebody help me?

Upvotes: 2

Views: 1227

Answers (1)

Gihan
Gihan

Reputation: 4283

You cannot use CDbCriteria, Try using query builder.

Yii::app()->db->createCommand()
->select('P.id, P.name, V.time')
->from('products P, views V')
->where('P.type = :type')
->order('V.time DESC')
->queryAll(array(
    ':type' => 2
));

Upvotes: 2

Related Questions