CakePHP模型关联:查看产品图片
我有一个产品模型,每个产品都有几张图片。所以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 Products
controller 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用烘焙脚本时,您的索引操作中很可能会包含以下行:
这将禁用您设置的所有关系。注释/删除此行,您应该能够正确显示图像(它应该位于 $this->data['Product']['Image'] 下)。
When you have used the bake script, your index action will most likely have this line in it:
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']).