输出值不等于yii2中的某些值
我希望输出不等于某些值的变量,但它返回的错误为输出值不等于yii2中的某些值,yii,yii2,yii-extensions,Yii,Yii2,Yii Extensions,我希望输出不等于某些值的变量,但它返回的错误为 Failed to prepare SQL: SELECT * FROM `tblsuunit` WHERE `unitid` != :qp0 有两个模型第一个模型是我获取ID数组的模型 public function actionSunits($id){ $unitslocation = new Unitslocation(); $id2 = Unitslocation::find()->where(['officeloc
Failed to prepare SQL: SELECT * FROM `tblsuunit` WHERE `unitid` != :qp0
有两个模型第一个模型是我获取ID数组的模型
public function actionSunits($id){
$unitslocation = new Unitslocation();
$id2 = Unitslocation::find()->where(['officelocationid'=>$id])->all();
foreach( $id2 as $ids){
print_r($ids['unitid']."<br>");
}
}
然后,我想获取id并比较另一个模型(units模型),得到与上面不相似的id值,然后输出
所以我补充说
$idall = Units::find()->where(['!=', 'unitid', $ids])->all();
所以整个控制器的动作就变成了
public function actionSunits($id){
$unitslocation = new Unitslocation();
$id2 = Unitslocation::find()->where(['officelocationid'=>$id])->all();
foreach( $id2 as $ids){
$idall = Units::find()->where(['!=', 'unitid', $ids])->all();
}
var_dump($idall);
}
这是单位模型表:
如果它正在工作,它应该返回7和10
可能有什么问题。尝试:
$idall = Units::find()->where(['not in','unitid',$ids])->all();
信息:
操作数1应为列或DB表达式。操作数2可以是
数组或查询对象
您应该修复代码,只需使用不处于状态的
,例如:
// $uls will be an array of Unitslocation objects
$uls = Unitslocation::find()->where(['officelocationid'=>$id])->all();
// $uids will contain the unitids
$uids = \yii\helpers\ArrayHelper::getColumn($uls, 'unitid');
// then simply use a not in condition
$units = Units::find()->where(['not in', 'unitid', $uids])->all();
$idall = \yii\helpers\ArrayHelper::getColumn($units, 'unitid');
阅读更多关于和的信息
// $uls will be an array of Unitslocation objects
$uls = Unitslocation::find()->where(['officelocationid'=>$id])->all();
// $uids will contain the unitids
$uids = \yii\helpers\ArrayHelper::getColumn($uls, 'unitid');
// then simply use a not in condition
$units = Units::find()->where(['not in', 'unitid', $uids])->all();
$idall = \yii\helpers\ArrayHelper::getColumn($units, 'unitid');