我目前正試圖解決一個問題,我只是注意到,因爲該屬性並沒有實際上有一個main_image分配給它。Cakephp協會屬性圖片,但也有很多圖像
但我一分配一個主圖像。相關圖片引發了問題。
屬性表中有一個屬於關聯關係
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>
產生的觀點,我不知道,如果可能的關係設置不正確,或者也許我將不得不包括如果要查看數組之前是否有數據,然後執行for循環。
不應該你的'圖像屬於屬性'和'屬性hasMany圖像'? – skywalker
@skywalker但如果我這樣做,那麼我不會有詳細信息main_image_id – Enzero
您的主圖像可以在Images表中具有'main_image'這樣的列,並且可以是'tinyint'類型,並將其設置爲1,然後將其設置爲1有它的主要形象與該「財產」的其他圖像,我認爲這是你的最佳解決方案。 – skywalker