2014-03-13 24 views
0

我目前正試圖解決一個問題,我只是注意到,因爲該屬性並沒有實際上有一個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循環。

+0

不應該你的'圖像屬於屬性'和'屬性hasMany圖像'? – skywalker

+0

@skywalker但如果我這樣做,那麼我不會有詳細信息main_image_id – Enzero

+0

您的主圖像可以在Images表中具有'main_image'這樣的列,並且可以是'tinyint'類型,並將其設置爲1,然後將其設置爲1有它的主要形象與該「財產」的其他圖像,我認爲這是你的最佳解決方案。 – skywalker

回答

1

我會回答,而不是評論。你會得到所有的屬性和它們類似這樣的主圖像(這發生在你的控制器動作如指數()。):

$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],但你可以很容易地解決這個問題。

+0

在哪裏將查找放置在視圖或控制器中。 – Enzero

+0

當然在控制器中。查看更新的答案。 – skywalker

+0

我不知道如何我可以在Paginator設置中實現此 – Enzero