Yii2,如何通过查询检查关系?
Yii2, how to check relation by a query?
我有两个模型,通过连接点 table 建立关系。所以,我试图从数据库中获取一些没有这种关系的模型。
我可以通过 做到这一点。
但是如何通过查询来做到这一点?使用 with 或 joinWith 方法,并检查 IS NULL
at junction table columns ?
创建路口模型并添加到您的 AR 模型方法中:
/**
* @return ActiveQuery
*/
public function getJunctions()
{
return $this->hasMany(Junction::className(), ['someId' => 'id']);
}
然后你可以将它与查询一起使用:
$query = Model::find()
->joinWith([
'junctions' => function (\yii\db\ActiveQuery $query) {
$query->andWhere(['{{junction}}.id' => null]);
}
], false);
我有两个模型,通过连接点 table 建立关系。所以,我试图从数据库中获取一些没有这种关系的模型。
我可以通过 IS NULL
at junction table columns ?
创建路口模型并添加到您的 AR 模型方法中:
/**
* @return ActiveQuery
*/
public function getJunctions()
{
return $this->hasMany(Junction::className(), ['someId' => 'id']);
}
然后你可以将它与查询一起使用:
$query = Model::find()
->joinWith([
'junctions' => function (\yii\db\ActiveQuery $query) {
$query->andWhere(['{{junction}}.id' => null]);
}
], false);