在具有多个 where 子句的 Active Record 中使用 find()
Using find() in Active Record with multiple where clause
我想(使用括号)将 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');
我还为您重构了它,现在它看起来更易读了。不要那样使用串联,这不是好的做法。
我想(使用括号)将 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');
我还为您重构了它,现在它看起来更易读了。不要那样使用串联,这不是好的做法。