我尝试构建具有多对多关系的网格视图 . 所以我需要查询 ActiveDataProvider
.
我有一个表'ressource',一个表'type',在它们之间有一个表'historique' .
我在模型中有很好的关系,但我不知道如何创建dataProvider .
在我的模型Ressource:
public function getHistorique()
{
return $this->hasMany(Historique::className(), ['idType' => 'idType']);
}
public function getType()
{
return $this->hasMany(Type::className(), ['idType' => 'idType'])
->viaTable(Historique::className(), ['idRessource' => 'idRessource']);
}
在我的模型Historique中:
public function getType()
{
return $this->hasOne(Type::className(), ['idType' => 'idType']);
}
public function getRessource()
{
return $this->hasOne(Ressource::className(), ['idRessource' => 'idRessource']);
}
最后在我的模型中输入:
public function getHistorique()
{
return $this->hasMany(Historique::className(), ['idType' => 'idType']);
}
public function getRessource()
{
return $this->hasMany(Ressource::className(), ['idRessource' => 'idRessource'])
->viaTable(Historique::className(), ['idType' => 'idType']);
}
所以在Controller(实际上是我的ModelSearch)中,我希望从表历史中获得具有类型的资源 . 我不知道我要添加什么
Ressource::find();
1 回答
我认为你使用
RessourceSearch()->search()
方法 . 所以在里面你有这样的东西:因此,基本上,您需要在查询中添加额外的
Where
并强制加入关系表 . 您可以使用joinWith
方法连接其他关系,andFilterWhere
使用table.field
表示法来添加过滤器参数 . 例如:另外,请不要忘记在搜索模型中添加其他过滤器的规则 . 例如,您应该在
rules()
数组中添加以下内容:您可以对该字段使用任何其他验证规则