Yii2 缩短 where() 和 orWhere()
Yii2 Shorten where () and orWhere ()
来自 的参考资料是否有另一种缩短 where() 和 orWhere() 的方法?
例如代码:
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(['Status' => 'Pasif'])
->orWhere(['Status' => 'Rolling'])
->asArray()->all(), 'Id', 'Shift'
);
[更新] 已解决
我认为比较两个或更多值的同一列的最佳解决方案是使用 IN Condition
,我们可以清楚地看到详细代码,或者您也可以使用 OR
或直接构建它喜欢 ->where(['Status' => ['Pasif', 'Rolling']])
代码 IN
:
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'IN',
'Status',['Pasif', 'Rolling']
]
)
->asArray()->all(), 'Id', 'Shift'
);
通过我的手机发布,您希望通过以下方式进行
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'OR',
['Status' => 'Pasif'],
['Status' => 'Rolling'],
]
)->asArray()->all(),
'Id',
'Shift'
);
因为您正在比较同一列的两个值。我想 IN Condition
也可以。
HrEmployeeShift::find()
->where(['Status' => ['Pasif', 'Rolling']])
->asArray()
->all();
或
HrEmployeeShift::find()
->where(['IN', 'Status', ['Pasif', 'Rolling']])
->asArray()
->all();
这将导致条件 Status IN ('Pasif', 'Rolling')
。
来自
例如代码:
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(['Status' => 'Pasif'])
->orWhere(['Status' => 'Rolling'])
->asArray()->all(), 'Id', 'Shift'
);
[更新] 已解决
我认为比较两个或更多值的同一列的最佳解决方案是使用 IN Condition
,我们可以清楚地看到详细代码,或者您也可以使用 OR
或直接构建它喜欢 ->where(['Status' => ['Pasif', 'Rolling']])
代码 IN
:
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'IN',
'Status',['Pasif', 'Rolling']
]
)
->asArray()->all(), 'Id', 'Shift'
);
通过我的手机发布,您希望通过以下方式进行
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'OR',
['Status' => 'Pasif'],
['Status' => 'Rolling'],
]
)->asArray()->all(),
'Id',
'Shift'
);
因为您正在比较同一列的两个值。我想 IN Condition
也可以。
HrEmployeeShift::find()
->where(['Status' => ['Pasif', 'Rolling']])
->asArray()
->all();
或
HrEmployeeShift::find()
->where(['IN', 'Status', ['Pasif', 'Rolling']])
->asArray()
->all();
这将导致条件 Status IN ('Pasif', 'Rolling')
。