我想将 Active Record 查询后的内容(使用括号)分为 3 组。第一组是从第一个“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');
可以这样做:
$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');
我还为您重构了它,使其现在看起来更具可读性。不要使用这样的串联,这不是一个好的做法。
See 官方文档 http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where%28%29-detail.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)