使用 CGridView 进行模型关联
我有一个具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
去掉课程后的括号
使用数组数据提供程序
Get rid of the parentheses after courses
Use an arraydataprovider
首先有几件事:
$model->courses
而不是$model->courses()
不管怎样,我似乎也记得在这个问题上挣扎过。我从来没有真正让它按照我想要的方式工作。我得到的最接近的是在 dataProvider 中设置“data”变量,但随后 ListView 中的分页被破坏。它的工作原理是这样的:
我最终做的是创建新的标准来传递给我的 DataProvider,它与关系做了同样的事情。就像这样:
我假设您正在使用带有连接表的 MANY_MANY 关系。我希望这会有所帮助,尽管我确信这不完全是您想要做的。如果有人有更好的解决方案请分享!我也想了解一下! :)
A few things to start off:
$model->courses
instead of$model->courses()
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:
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:
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! :)