使用 CGridView 进行模型关联

发布于 2024-10-31 19:56:07 字数 980 浏览 3 评论 0原文

我有一个具有 has_many 关联的模型。

假设学生有很多课程。

我想使用 CGridView 显示特定学生的所有课程。

像这样的事情:

$this->widget('zii.widgets.grid.CGridView', array(                                                 
  'dataProvider' => $model->courses,                                                             
  'columns'=>array(                                                                                                                                                                            
    'name',                                                                                                                                                                                  
  ),                                                                                                 
));

还尝试了 new CActiveDataProvider($model->courses) 作为 dataProvider 但仍然无法工作。

有没有简单的方法可以做到这一点?或者我是否必须在课程模型上创建搜索条件,并手动从学生模型中获取一些条件?

I have a model with has_many association.

Let's just say Student has many Courses.

I'd like to show all courses of a particular student using CGridView.

Something like this:

$this->widget('zii.widgets.grid.CGridView', array(                                                 
  'dataProvider' => $model->courses,                                                             
  'columns'=>array(                                                                                                                                                                            
    'name',                                                                                                                                                                                  
  ),                                                                                                 
));

Also tried new CActiveDataProvider($model->courses) as dataProvider but still wont work.

Is there an easy way to do this? Or do I have to create a search criteria on the Course model with some criteria taken from the student model manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-11-07 19:56:07
  1. 去掉课程后的括号

  2. 使用数组数据提供程序

    $this->widget('zii.widgets.grid.CGridView', array(
      '数据提供者' => new CArrayDataProvider($model->courses, array()),
      '列'=>数组(
        '姓名',
      ), 
    ));
    
  1. Get rid of the parentheses after courses

  2. Use an arraydataprovider

    $this->widget('zii.widgets.grid.CGridView', array(
      'dataProvider' => new CArrayDataProvider($model->courses, array()),
      'columns'=>array(
        'name',
      ), 
    ));
    
她说她爱他 2024-11-07 19:56:07

首先有几件事:

  1. 我认为应该是 $model->courses 而不是 $model->courses()
  2. 无论如何都不起作用的原因是 Yii 的 AR 关系返回一个对象数组,而不是实际的 DataProvider 对象

不管怎样,我似乎也记得在这个问题上挣扎过。我从来没有真正让它按照我想要的方式工作。我得到的最接近的是在 dataProvider 中设置“data”变量,但随后 ListView 中的分页被破坏。它的工作原理是这样的:

$dataProvider=new CActiveDataProvider('Course', array(
  'data'=>$model->courses,
));

我最终做的是创建新的标准来传递给我的 DataProvider,它与关系做了同样的事情。就像这样:

$criteria=new CDbCriteria;
$criteria->join = "JOIN student_course_relation_table sc ON sc.course_id = t.id";
$criteria->compare('sc.student_id',$model->id);
$dataProvider=new CActiveDataProvider('Course', array(
  'criteria'=>$model->courses,
));

我假设您正在使用带有连接表的 MANY_MANY 关系。我希望这会有所帮助,尽管我确信这不完全是您想要做的。如果有人有更好的解决方案请分享!我也想了解一下! :)

A few things to start off:

  1. I think it should be $model->courses instead of $model->courses()
  2. The reason that doesn't work anyway is that Yii's AR relations return an array of objects, not an actual DataProvider object

Anyway, I seem to remember struggling with this as well. I never really got it to work like I wanted it to. The closest I got was setting the 'data' variable in my dataProvider, but then the paging was broken in my ListView. It worked something like this:

$dataProvider=new CActiveDataProvider('Course', array(
  'data'=>$model->courses,
));

What I ended up doing was creating new criteria to pass in to my DataProvider that did the same thing as the relation. Like so:

$criteria=new CDbCriteria;
$criteria->join = "JOIN student_course_relation_table sc ON sc.course_id = t.id";
$criteria->compare('sc.student_id',$model->id);
$dataProvider=new CActiveDataProvider('Course', array(
  'criteria'=>$model->courses,
));

I'm assuming that you are using a MANY_MANY relationship with a join table. I hope this helps, even though I'm sure it's not quite what you want to do. If anyone has a better solution please share! I want to learn about it too! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文