Yii - CDbCriteria意外结果

时间:2014-02-06 12:46:42

标签: database yii frameworks

我正在做的看起来像一个简单的查询基本上在competition_id和prize_type

上做一个WHERE子句
    $criteria = new CDbCriteria;
    $criteria->select = 't.*, myuser.firstname, myuser.surname';
    $criteria->join ='LEFT JOIN myuser ON myuser.user_id = t.user_id';
    $criteria->condition = 't.competition_id = :competition_id';
    $criteria->condition = 't.prize_type = :prize_type';
    $criteria->params = array(":competition_id" => $competition_id);
    $criteria->params = array(":prize_type" => "1");

    $winners = CompetitionWinners::model()->findAll($criteria);

任何人都可以建议我的代码有什么问题...我期待大约4行..但是超过600行?

我只是想......

WHERE competition_id = 123 AND prize_type = 1;

是否有一个简单的函数来简单地输出这个SINGLE CDbCriteria'事件'的SQL查询?

2 个答案:

答案 0 :(得分:1)

试试这个

$criteria = new CDbCriteria;
    $criteria->select = 't.*, myuser.firstname, myuser.surname';
    $criteria->join ='LEFT JOIN myuser ON myuser.user_id = t.user_id';
    $criteria->condition = 't.competition_id = :competition_id AND t.prize_type = :prize_type';
    $criteria->params = array(":competition_id" => $competition_id,":prize_type" => "1");
    $winners = CompetitionWinners::model()->findAll($criteria);

答案 1 :(得分:1)

或者您可以使用CDbCriteria::addCondition()

$criteria->addCondition('t.competition_id = :competition_id')
         ->addCondition('t.prize_type = :prize_type');