在yii中创建CDBcriteria

时间:2014-02-05 12:32:56

标签: yii yii-components yii-events

如何为查询创建cdbcriteria,如:

select * from table_name where 'profile_type'='*'OR 'profile_type'=$usertype  AND 'location'='*'OR 'location'=$country

2 个答案:

答案 0 :(得分:1)

您可以直接传递条件如下。

注意:这是方法之一。不是最终的解决方案。

$criteria = new CDbCriteria;
$criteria->condition  = "(profile_type ='*' OR profile_type = $usertype)  AND (location ='*' OR location = $country)";

$model = Model_name::model()->findAll($criteria );

答案 1 :(得分:1)

你可以试试这样:

$criteria = new CDbCriteria;
$criteria->condition  = "(profile_type='*' OR profile_type=:prof ) AND 
                         (location='*' OR  location=:loc ) ";

$criteria->params = array(':prof' => $usertype, ':loc' => $country);

$model = MyModel::model()->findAll($criteria );