CakePHP模型关联:查看产品图片

发布于 2024-12-26 04:26:00 字数 550 浏览 1 评论 0原文

我有一个产品模型,每个产品都有几张图片。所以Product-hasMany-Image

现在,当我浏览某些产品时,我可以看到它所具有的图像,但当我在索引中列出产品时无法查看图像。 我应该如何从产品中获取第一张图像以将其显示在索引中?

我能处理这个问题的唯一方法是在 Products 控制器中使用 Image 模型,但我想这不是最好的选择。

编辑: 大多数东西就像烘烤后一样。默认情况下,列出所有产品时是否显示 hasMany?

模型:

class Product extends AppModel {
var $hasMany = array(
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'parent_id',
        'dependent'=> false
    ),
);    }

I have a products model, and each product has several images. So Product-hasMany-Image.

Now when I browse some product I am able to see the images that it has but not able to view the images when I list the products in the index.
How should I get the first image from the product to display it in the index?

The only way I can manage to deal with this is to use the Image model inside the Productscontroller but I guess that this is not the best option.

EDIT:
Most of the stuff is just like after baked. By default does the hasMany display when listing all the products?

Model:

class Product extends AppModel {
var $hasMany = array(
    'Image' => array(
        'className' => 'Image',
        'foreignKey' => 'parent_id',
        'dependent'=> false
    ),
);    }

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

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

发布评论

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

评论(1

少女净妖师 2025-01-02 04:26:00

当您使用烘焙脚本时,您的索引操作中很可能会包含以下行:

$this->Product->recursive = 0;

这将禁用您设置的所有关系。注释/删除此行,您应该能够正确显示图像(它应该位于 $this->data['Product']['Image'] 下)。

When you have used the bake script, your index action will most likely have this line in it:

$this->Product->recursive = 0;

This will disable all the relations that you've set. Comment/remove this line and you should be able to display the image properly (it should be under $this->data['Product']['Image']).

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