Cakephp关联属性属于图像,但也有许多图像
我目前正在尝试解决一个我刚刚注意到的问题,因为该属性实际上没有分配主映像 但一旦我给它分配了一个主图像。相关图像引起了问题 属性表具有belongsTo关系Cakephp关联属性属于图像,但也有许多图像,cakephp,cakephp-2.4,Cakephp,Cakephp 2.4,我目前正在尝试解决一个我刚刚注意到的问题,因为该属性实际上没有分配主映像 但一旦我给它分配了一个主图像。相关图像引起了问题 属性表具有belongsTo关系 public $belongsTo = array( 'Image' => array( 'className' => 'Image', 'foreignKey' => 'main_image_id' )); 而且有很多 public $hasMany =
public $belongsTo = array( 'Image' => array(
'className' => 'Image',
'foreignKey' => 'main_image_id'
));
而且有很多
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循环。我将回答它,而不是评论。您将获得所有属性及其主映像,如下所示(将其放置在控制器操作中,例如index()): 在你看来:
<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>
'style='max-width:100%;高度:150px;;'>
在最后一个问题上,不要肯定我的观点,我正在绞尽脑汁,我想你需要[0],但是你可以很容易地解决这个问题。你的
图像不应该属于属性
并且属性有很多图像
?@skywalker但是如果我这样做了,那么我就没有主图像的详细信息了。你的主图像可以在图像表中有类似主图像
的列,并且可以是tinyint
类型,只需将其设置为1和这里有它的主图像和该属性的其他图像,我认为这是最适合您的解决方案。@skywalker那么我如何显示当前显示的图像,就像“style='max-width:100%;高度:150px;;'>我不知道如何在Paginator设置中实现这一点。我正在检索这样的属性$this->Paginator->settings=array('conditions'=>array($conditionID、$categoryID、$localityID、$serviceTypeID、'Property.price介于“$params['priceFrom']”和“$params['priceTo']”)之间,'limit'=>15);我还尝试了你的代码,它仍然返回了两个图像,即使其中一个图像不是main,然后只需获取包含
数组并将其添加到paginator设置数组中。对于两个图像,请将该属性的所有其他图像设置为具有主图像
0。
<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>