Yii - 一页中的多个 CGridView 关系

发布于 2025-01-05 08:10:00 字数 334 浏览 0 评论 0原文

我有以下关系方案:

1 投资组合 ->每个投资组合中有许多用户 ->每个用户拥有许多产品

实体:投资组合、用户、产品

我想在一页中显示: 1. 投资组合名称 2. 用户的头衔 3. 该用户拥有的所有产品的 CGridView

看来,如果未使用 CGridView,您将适当地填充您的模型,然后使用 foreach 循环遍历每个用户关系,然后循环遍历与该用户相关的产品。并为产品和用户创建一个 render_partial 视图。

但是,我完全不知道如何使用 CGridView 来完成此任务。

任何建议都非常非常感谢!

I have the following relation scheme:

1 portfolio -> Many Users in each Portfolio -> Each User owns many Products

Entities: Portfolio, User, Products

What I would like to display is in one page:
1. The name of the portfolio
2. The title of a user
3. CGridView of all products owned by that user

It seems, if CGridView were not being used, you would populate your model appropriately and then use a foreach loop to loop through each user relation and then loop through the products related to that user. And create a render_partial view for the products and for the user.

However, I am at a complete loss as how to accomplish this with CGridView.

Any advice is much, much appreciated!

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

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

发布评论

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

评论(1

っ左 2025-01-12 08:10:00

您想在 CGridView 中呈现用户的产品吗?

要使用 CGridView 呈现数据,您需要传入 CDataProvider 来填充视图。所以问题是:如何为 HAS_MANY 关系创建 CDataProvider?

令人惊讶的是,CActiveDataProvider 不支持这种方式的关系。您需要做什么来获取关系并将该数据传递到 CArrayDataProvider 中。假设$user->products关系,你可以这样做:

$dataProvider = new CArrayDataProvider($user->products, array());
$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
    'id', // your columns here
  ),
));

它并不理想,但它有效。此特定技术的功劳如下: http ://learnyii.blogspot.com/2010/12/yii-how-to-display-lated-hasmany-grid.html

You want to render the User's Products in a CGridView?

To render data with a CGridView you need to pass in a CDataProvider to populate the view. So the question is: how do you make a CDataProvider for the HAS_MANY relation?

Surprisingly, the CActiveDataProvider does not support relations in this way. What you need to do get the relation and pass that data into a CArrayDataProvider. Assuming the $user->productsrelation,you can do it like this:

$dataProvider = new CArrayDataProvider($user->products, array());
$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
    'id', // your columns here
  ),
));

It's not ideal, but it works. Credit goes here for this specific technique: http://learnyii.blogspot.com/2010/12/yii-how-to-display-related-hasmany-grid.html

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