CakePHP查找相关数据
我的图库模型与我的图像模型相关,但当我从图库\u控制器检索图像时,我看不到任何结果,而是看到以下错误: 警告(2):为foreach()提供的参数无效[APP\views\galleries\view.ctp,第35行] 以下是我的控制器操作的代码:CakePHP查找相关数据,cakephp,cakephp-2.0,cakephp-model,Cakephp,Cakephp 2.0,Cakephp Model,我的图库模型与我的图像模型相关,但当我从图库\u控制器检索图像时,我看不到任何结果,而是看到以下错误: 警告(2):为foreach()提供的参数无效[APP\views\galleries\view.ctp,第35行] 以下是我的控制器操作的代码: function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid gallery',
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; ?>
您的电视机没有任何设置。您的查找正在执行,但未存储在变量中。此外,视图循环正在错误地迭代 将控制器更改为:
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));