Cakephp foreach

时间:2016-04-24 04:31:17

标签: cakephp foreach

当我使用foreach在基数中显示我的值时,它会向我显示一个错误的值 在控制器中:

    public function index_hote() {
    $this->Reservation->recursive = 1;
    $this->loadModel("Bien");
    $biens=$this->Bien->find('first',array('conditions'=>array("Bien.idBien     =idBien")));
     $reservations=$this->Reservation-   >find('first',
     array('conditions'=>array('Reservation.idBien'=> $biens['Bien'] ['idBien'])));
      Debugger::dump($reservations['Reservation']);
    $this->paginate('Reservation');
   $this->set('reservations', $reservations['Reservation']);

    }

在视图中:

    <?php foreach ($reservations as $reservation): ?>
     <tr>   
     <?php echo h($reservation) ?>
     <td><?php echo h($reservation['dateReserDu']); ?>&nbsp;</td>
    <td><?php echo h($reservation['dateReserAu']); ?>&nbsp;</td>
    <td><?php echo h($reservation['montantAPaye']); ?>&nbsp;</td>
    <td><?php echo h($reservation['etatReservation']); ?>&nbsp;</td>
      </tr>
     <?php endforeach; ?>

当我调试时,它显示了正确的值(执行Debugger::dump($reservations['Reservation']);后的图片):

the pic after doing  Debugger::dump($reservations['Reservation']);

但是在表格中它显示了一个假值:

pic of the false value

当我执行<?php echo h($reservation) ?>时,它会向我显示正确的值,但是当我添加h($reservation['dateReserDu']); ?>这样的属性时,它不能用于PLZ,为什么?我什么时候可以做?

1 个答案:

答案 0 :(得分:1)

为什么你在这里使用循环?您已使用find('first')检索一个数据 doc

如果要使用find('all')检索所有数据。但是,如果您想echo找到第一个数据,请使用以下结构

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

在视图中

echo $reservations['Reservation']['Your_field_name'];