doubo4824
2015-07-02 20:35
浏览 121
已采纳

yii2 - 多对多关系的CRUD

I am new to yii. I have tried myself,googled and found that yii2 default CRUD generator Gii will not generate CRUD for tables which has many to many relations. Also found that yii achieves(not in the sense Gii) Many to Many via One to Many yiiDrills .

Now I am trying to emulate the same kind of default CRUD manually with the help of Github issue trail and stackoverflow trail. I am facing the below issues while trying this.

Issue-1 (Model class of the table with Many to Many relations): Not able to initialize the class ActiveDataProvider,

   $query = TableA::find();
   $dataProvider = new ActiveDataProvider([
        'query' => $query->TableB(),
    ]);

Issue-2(View): Even if I were able to initialize it how to render it via GridView

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
  //How to give the column names like we give for one to many
  /* Example */
       'TableA.attr1',
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

Also I would like to know if it is desired to create a Model class for the table with Many to Many relations to handle CRUD.

Thanks

图片转代码服务由CSDN问答提供 功能建议

我是yii的新手。 我试过自己,用Google搜索并发现yii2默认的CRUD生成器Gii不会为具有多对多关系的表生成CRUD。 还发现yii实现了(不是Gii意义上的)多对多来自一对多 yiiDrills

现在我试图在手的帮助下手动模拟相同类型的默认CRUD Github问题跟踪 stackoverflow trail 。 我在尝试这个时遇到了以下问题。

Issue-1(具有多对多关系的表的模型类):无法初始化类ActiveDataProvider, \ n

  $ query = TableA :: find(); 
 $ dataProvider = new ActiveDataProvider([
'query'=&gt; $ query-&gt; TableB(),
]);  
   
 
 

Issue-2(View):即使我能够初始化它如何通过GridView渲染它

 <  code>&lt;?= GridView :: widget([
'dataProvider'=&gt; $ dataProvider,
'filterModel'=&gt; $ searchModel,
'columns'=&gt; [
 ['class'=  &gt;'yii \ grid \ SerialColumn'],
 //如何给出列名,就像我们给出的一对多
 / *示例* / 
'TableA.attr1',
 ['class'=  &gt;'yii \ grid \ ActionColumn'],
],
]);  ?&gt; 
   
 
 

此外,我想知道是否需要为具有多对多关系的表创建一个Model类来处理CRUD。

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpan1308 2015-07-03 10:24
    已采纳

    You should create models for all tables.

    Example relations

    /**
    * @return \yii\db\ActiveQuery
    */
    public function getCountry()
    {
      return $this->hasOne(Country::className(), ['id' => 'country_id']);
    }
    
    /**
    * @return \yii\db\ActiveQuery
    */
    public function getCity()
    {
       return $this->hasOne(City::className(), ['id' => 'city_id']);
    }
    

    Example "Search" method

    $query = Tour::find();
    // Important: lets join the query with our previously mentioned relations
    // I do not make any other configuration like aliases or whatever, feel free
    // to investigate that your self
    $query->joinWith(['city', 'country']);
    
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    

    You can replace hasOne with hasMany in your case.

    Please check the link http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题