Yii 1查询返回错误数据

时间:2018-01-08 12:23:40

标签: php mysql database yii yii1.x

我有疑问:

$listings = Yii::app()->db->createCommand('SELECT * FROM listings')->where(['or like','c_sales_stage',['Needs Refresh','Active']])->andWhere('c_listing_featured_c = 1')->queryAll();

即使c_listing_featured_c为0,也会返回所有列表。 我做错了什么?

由于

1 个答案:

答案 0 :(得分:2)

正如documentation所说:

注意:“查询”构建器不能用于修改指定为SQL语句的现有查询。例如,以下代码无效:

$command = Yii::app()->db->createCommand('SELECT * FROM tbl_user');
// the following line will NOT append WHERE clause to the above SQL
$command->where('id=:id', array(':id'=>$id));

要解决您的问题,请从createCommand()函数中删除参数并在链中添加from()

$listings = Yii::app()->db->createCommand()
   ->from('listings')
   //->where()       //here your where condition
   ->andWhere('c_listing_featured_c = 1')
   ->queryAll();