Cakephp Association属性属于Image但也有很多图像

时间:2014-03-13 14:19:42

标签: cakephp cakephp-2.4

我目前正在尝试解决一个我刚注意到的问题,因为该属性实际上没有为其分配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循环。

1 个答案:

答案 0 :(得分:1)

我会回答而不是评论。您将获得所有属性及其主要图像(将其置于控制器操作中,例如index()):

$properties= $this->Property->find('all', array(
    'contain' => array(
        'Image' => array(
            'conditions' => array(
                'main_image' => true
            )
        )
    )
));

$this->set('properties', $properties);

并在您看来:

<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>

不要在最后一个问题上采取我的行动,我正在考虑这个问题,我认为你需要[0],但你可以很容易地解决这个问题。