Yii2:如何编写以下活动查询?
Yii2: How do I write the below Active Query?
使用 this solution,如何将我的 $conditions
变量用于我的活动查询?
$conditions = 'main_category_id != :main_category_id', ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id,'type'=> 4];
$result = Category::find()->where($conditions)->orderby('category_name ASC')->all();
根据我的查询 main_category_id != 0
。任何其他有效的解决方案也可以
请注意,我需要 $conditions
变量,因为它们各不相同。这是我的 if 语句查询:
public function get_subcategory_list($id="",$type="")
{
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'main_category_id' => $id, 'type' => 2];
if($type == 2){
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id, 'type' => 3];
}
if($type == 3){
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id,'type'=> 4];
}
$result = Category::find()->where($conditions)->orderby('category_name ASC')->all();
return $result;
}
请注意 $conditions
在上述函数上工作正常,唯一的问题是 main_category_id
不应该等于 0。
您不能像您那样分配变量 ($conditions
),它是无效的。
你的ActiveQuery
可以这样写:
$models = Category::find()
->where(['<>', 'main_category_id', 0])
->andWhere([
'category_status' => 1,
'sub_category_id' => $subCategoryId,
'type'=> 4,
])
->orderBy(['category_name' => SORT_DESC])
->all();
如果使用主要类别 ID 数组,您可以将 <>
替换为 !=
或 not in
。
在 official docs 中阅读有关构造 where
条件的更多信息。
更新: 无需更改整个 $conditions
数组和复制粘贴。例如,您可以这样计算 $type
:
switch ($type) {
case 2:
$typeValue = 3;
break;
case 3:
$typeValue = 4;
break;
default:
$typeValue = 2;
}
这个逻辑有点奇怪(也许自增+1会更好)。
然后只需在此处插入 $typeValue
而不是静态值 'type'=> $typeValue
。
即使您有一些复杂的查询构造,您也可以将查询分成单独的 where
/ orWhere
/ andWhere
并动态更改它。
使用 this solution,如何将我的 $conditions
变量用于我的活动查询?
$conditions = 'main_category_id != :main_category_id', ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id,'type'=> 4];
$result = Category::find()->where($conditions)->orderby('category_name ASC')->all();
根据我的查询 main_category_id != 0
。任何其他有效的解决方案也可以
请注意,我需要 $conditions
变量,因为它们各不相同。这是我的 if 语句查询:
public function get_subcategory_list($id="",$type="")
{
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'main_category_id' => $id, 'type' => 2];
if($type == 2){
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id, 'type' => 3];
}
if($type == 3){
$conditions = ['category_status' => 1, 'main_category_id' => 0, 'sub_category_id' => $id,'type'=> 4];
}
$result = Category::find()->where($conditions)->orderby('category_name ASC')->all();
return $result;
}
请注意 $conditions
在上述函数上工作正常,唯一的问题是 main_category_id
不应该等于 0。
您不能像您那样分配变量 ($conditions
),它是无效的。
你的ActiveQuery
可以这样写:
$models = Category::find()
->where(['<>', 'main_category_id', 0])
->andWhere([
'category_status' => 1,
'sub_category_id' => $subCategoryId,
'type'=> 4,
])
->orderBy(['category_name' => SORT_DESC])
->all();
如果使用主要类别 ID 数组,您可以将 <>
替换为 !=
或 not in
。
在 official docs 中阅读有关构造 where
条件的更多信息。
更新: 无需更改整个 $conditions
数组和复制粘贴。例如,您可以这样计算 $type
:
switch ($type) {
case 2:
$typeValue = 3;
break;
case 3:
$typeValue = 4;
break;
default:
$typeValue = 2;
}
这个逻辑有点奇怪(也许自增+1会更好)。
然后只需在此处插入 $typeValue
而不是静态值 'type'=> $typeValue
。
即使您有一些复杂的查询构造,您也可以将查询分成单独的 where
/ orWhere
/ andWhere
并动态更改它。