【发布时间】:2014-03-13 14:19:42
【问题描述】:
我目前正在尝试解决一个我刚刚注意到的问题,因为该属性实际上没有分配给它的 main_image。
但是一旦我为它分配了一个主图像。相关图片引起了问题。
属性表有一个 belongsTo 关系
public $belongsTo = array( 'Image' => array(
'className' => 'Image',
'foreignKey' => 'main_image_id'
));
还有一个hasMany
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'property_id'
)
);
问题是当我填充相关图像时,我收到多个类似于以下的错误
非法字符串偏移'id'
我的数据是这样的
property(array)
Image(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
0(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
视图是这样生成的
<div class="related">
<h3><?php echo __('Related Images'); ?></h3>
<?php if (!empty($property['Image'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Id'); ?></th>
<th><?php echo __('Description'); ?></th>
<th><?php echo __('Image'); ?></th>
<th><?php echo __('Property Id'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($property['Image'] as $image): ?>
<tr>
<td><?php echo $image['id']; ?></td>
<td><?php echo $image['description']; ?></td>
<td><?php echo $image['image']; ?></td>
<td><?php echo $image['property_id']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('controller' => 'images', 'action' => 'view', $image['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('controller' => 'images', 'action' => 'edit', $image['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('controller' => 'images', 'action' => 'delete', $image['id']), null, __('Are you sure you want to delete # %s?', $image['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
我不确定关系是否设置不正确,或者我可能必须包含一个 if 来查看数组之前是否有数据,然后实现 for 循环。
【问题讨论】:
-
你的
Image belongsTo Property和Property hasMany Image不应该吗? -
@skywalker 但如果我这样做了,我就不会有 main_image_id 的详细信息
-
您的主图像可以在图像表中具有类似
main_image的列,并且可以是tinyint类型,只需将其设置为1,您就可以将其主图像与其他图像一起用于Property,我认为这是最适合您的解决方案。 -
@skywalker 那么我将如何显示我目前正在显示的图像,就像 ' style='max-width: 100%;height:150px;;'>
标签: cakephp cakephp-2.4