简体   繁体   English

具有多种关系的Yii2数据提供者

[英]Yii2 dataprovider with manytomany relation

I try to build a grid view with many-to-many relations. 我尝试构建具有多对多关系的网格视图。 So I need a query for the ActiveDataProvider . 所以我需要一个ActiveDataProvider的查询。

I have a table 'ressource', a table 'type' and between them a table 'historique'. 我有一个表'ressource',一个表'type',在它们之间有一个表'historique'。

I have the good relation in my models but I don't know how to create the dataProvider. 我在模型中有很好的关系,但我不知道如何创建dataProvider。

In my model Ressource : 在我的模型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']);   
}

In my model Historique : 在我的模型Historique中:

public function getType()
{
    return $this->hasOne(Type::className(), ['idType' => 'idType']);
}

public function getRessource()
{
    return $this->hasOne(Ressource::className(), ['idRessource' => 'idRessource']);
}

and finally in my model Type : 最后在我的模型中输入:

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']);
}

So in the Controller (in fact my ModelSearch), I want to have ressources with type from the table historique. 所以在Controller(实际上是我的ModelSearch)中,我希望从表history中获得类型的ressource。 I don't know what I have to add after 我不知道我要添加什么

Ressource::find();

I think you use RessourceSearch()->search() method. 我认为你使用RessourceSearch()->search()方法。 So inside it you have something like this: 所以在里面你有这样的东西:

$query = Ressource::find();

$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);

if (!($this->load($params) && $this->validate())) {
  return $dataProvider;
}

// Here is list of searchable fields of your model.
$query->andFilterWhere(['like', 'username', $this->username])
      ->andFilterWhere(['like', 'auth_key', $this->auth_key])


return $dataProvider;

So, basically, you need to add additional Where you your query and force to join relation table. 所以,基本上,你需要添加额外的Where ,你的查询和力量的加盟关系表。 You can do that using joinWith method to join additional relation and andFilterWhere using table.field notation for adding filter parameters. 您可以使用joinWith方法连接其他关系和andFilterWhere使用table.field表示法来添加过滤器参数。 For example: 例如:

$query = Ressource::find();
$query->joinWith(['historique', 'type']);
$query->andFilterWhere(['like', 'type.type', $this->type]);
$query->andFilterWhere(['like', 'historique.historique_field', $this->historique_field]);

Also do not forget to add rules for additional filters in your search model. 另外,请不要忘记在搜索模型中添加其他过滤器的规则。 For example above, you should add to your rules() array something like that: 例如,您应该在rules()数组中添加以下内容:

public function rules()
    {
        return [
            // here add attributes rules from Ressource model
            [['historique_field', 'type'], 'safe'],
        ];
    }

You can use any additional validation rules for that fields 您可以对该字段使用任何其他验证规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM