2012-01-11 86 views
0

我的畫廊模式是關係到我的圖像模型,但是當我retrive從galleries_controller我看不到任何結果,而是看這個錯誤圖片:CakePHP中查找相關數據

警告(二):提供對的foreach無效參數()[APP \視圖\畫廊\ view.ctp 35行]

這裏是從我的控制器操作的代碼:

function view($id = null) { 
      if (!$id) { 
       $this->Session->setFlash(__('Invalid gallery', true)); 
       $this->redirect(array('action' => 'index')); 
      } 
      $this->set('gallery', $this->Gallery->read(null,$id)); 
$this->Gallery->Image->find('all',array('conditions' => array('Image.gallery_id'=> $id))); 
      $this->set('images', $images); 

     } 

這是循環我在我的畫廊/視圖迭代通過陣列。

<?php 

     $i = 0; 
     foreach ($images['Image'] as $image): 
      $class = null; 
      if ($i++ % 2 == 0) { 
       $class = ' class="altrow"'; 
      } 
     ?> 
     <tr<?php echo $class;?>> 
      <td><?php $image['id'] ;?></td> 
      <td><?php echo $image['name'];?></td> 
      <!--<td><?php echo $image['img_file'];?></td>--> 

      <td><?php echo $html->image('uploads' . DS . 'images' . DS . $image['img_file'], array('alt' => 'Gallery Image')); ?></td> 

     </tr> 
    <?php endforeach; ?> 

回答

2

您沒有設置任何設置。您的查找正在執行,然後不存儲在變量中。此外,你的視圖循環通過不正確的迭代。

控制器更改爲:

function view($id = null) { 
      if (!$id) { 
       $this->Session->setFlash(__('Invalid gallery', true)); 
       $this->redirect(array('action' => 'index')); 
      } 
      $this->set('gallery', $this->Gallery->read(null,$id)); 
$images = $this->Gallery->Image->find('all',array('conditions' => array('Image.gallery_id'=> $id))); 
      $this->set('images', $images); 
     } 

而且您的視圖代碼:

<?php 

     $i = 0; 
     foreach ($images as $image): 
      $class = null; 
      if ($i++ % 2 == 0) { 
       $class = ' class="altrow"'; 
      } 
     ?> 
     <tr<?php echo $class;?>> 
      <td><?php $image['Image']['id'] ;?></td> 
      <td><?php echo $image['Image']['name'];?></td> 
      <!--<td><?php echo $image['Image']['img_file'];?></td>--> 

      <td><?php echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], array('alt' => 'Gallery Image')); ?></td> 

     </tr> 
    <?php endforeach; ?> 

話雖如此,因爲所有你想要做的是在相關圖像拉畫廊觀看時,你應該考慮建立你的模型關聯,然後在你的畫廊上找到一個包含你想要的圖像的例子:

$this->set('gallery',$this->Gallery->find('first', array('conditions' => array('Gallery.id' => $id), 'recursive' => 1)); 

如果上述不起作用,那麼您的模型中存在某些配置錯誤。請發佈您的模型代碼,以便我們可以進一步調查。

+0

謝謝swiecki它的工作 – user1080247 2012-01-11 09:03:02