The*_*der 4 php mysql yii yii2 yii2-advanced-app
我想在3组中的Active Record查询之后划分(使用括号).第一组将从第一个"Where"子句到最后一个"orWhere".第二和第三将使用"andWhere".
请给我一些关于如何使用括号分隔所有3个部分的建议.
$query = Book::find()
->where('book_name LIKE :book_name', array(':book_name' =>
'%'.$book_name.'%'))
->orWhere('book_category LIKE :book_category', array(':book_category' =>'%'.$category.'%'))
->orWhere('finance_subcategory LIKE :finance', array(':finance' => '%'.$category.'%'))
->orWhere('insurance_subcategory LIKE :insurance', array(':insurance' => '%'.$category.'%'))
->andWhere('address LIKE :address', array(':address' => '%'.$address.'%'))
->andWhere('status =:status', array(':status' => 'Enabled'))
->orderBy('book_id');
Run Code Online (Sandbox Code Playgroud)
aro*_*hev 13
可以这样做:
$query = Book::find()
->where([
'or',
['like', 'book_name', $book_name],
['like', 'book_category', $category],
['like', 'finance_subcategory', $category],
['like', 'insurance_subcategory', $category],
])
->andWhere(['like', 'address', $address])
->andWhere(['status' => 'Enabled'])
->orderBy('book_id');
Run Code Online (Sandbox Code Playgroud)
我也为你重构了它,所以它现在看起来更具可读性.不要使用这样的连接,这不是好习惯.
见官方文档.