控制器中未定义的索引(CakePhp)

问题描述:

我有以下代码,它根据id从表“产品”中选择产品。控制器中未定义的索引(CakePhp)

public function p_details(){ 
$productIdNum = $this->params['detailID']; 

     //$this->Product->read(null, $productIdNum); 

     if(Validation::naturalNumber($productIdNum) == true){ 
      $itemById = $this->Product->find('all', array('conditions' => 
      array('Product.id' => $productIdNum))); 

      if(count($itemById) > 0){ 
       $this->set('itemDetails', $itemById['Product']['id']); 
      } 
     } 

    } 

但是,当我尝试打印变量“$ itemDetails”中查看, 像<?php echo $itemDetails['Product']['name']; ?> 它给我这个错误: 未定义指数:产品。如果我将它更改为 就像<?php echo $itemDetails['name']; ?> 它仍然给我同样的错误:未定义的索引:名称。 我无法弄清楚这一点。

+0

可能的重复[参考 - 这个错误在PHP中意味着什么?](http://*.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nunser 2014-08-27 13:59:58

+0

下一次你得到索引找不到错误,你可以在你的视图中执行'debug($ array);'来查看你的数组实际包含了什么。 – schnauss 2014-08-27 21:47:38

嗯,问题是用$this->set('itemDetails', $itemById['Product']['id']);你给itemDetails变量分配一个id而不是数组。将其更改为$this->set('itemDetails', $itemById);,它应该可以工作。

+0

感谢您的回复。我后来注意到,尽管花了我近一个小时才弄清楚:)谢谢你的回复 – Michel 2014-08-28 12:30:51