【问题标题】:Cakephp Unidentified Index problemCakephp 未识别索引问题
【发布时间】:2013-09-27 01:53:05
【问题描述】:

您好多年来一直试图让它工作,

我有一个交易控制器

<?php
class DealsController extends AppController {

    var $name = 'Deals';
    var $helpers = array('HTML', 'Javascript', 'Form', 'Time', 'Ajax');
    var $components = array('RequestHandler', 'Session', 'Cookie');
    var $uses = array('Deal', 'City', 'Partner');

    function beforeFilter(){
        parent::beforeFilter();

        if(strpos($this->here, 'admin')){
            $this->layout = 'admin';
        }

    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid deal', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('deal', $this->Deal->read(null, $id));
    }

    function by_city($CityID = null)
{
    $city = $this->paginate = array(
        'conditions' => array('City.id' => $CityID),
        'limit' => 1
     );
    $this->set('city',$city);


    $this->Retailer->recursive = -1;
    $this->paginate = array(
        'conditions' => array('Deal.city_id' => $CityID),
        'order' => array('Deal.start'=>'desc'),
        'limit' => 3
     );
    $this->set('deals',$this->paginate());

}

by_city 视图是:

<h3>More Deals from <?php echo $city['City']['city'];?></h3>

现在,当我运行此程序时,我收到“未识别索引城市”通知

当我运行 var_dump 时,它说有一个数组 City 并且“city”也有一个值,这对我来说听起来就像控制器已经通过了这个

我现在迷路了,哈哈

感谢所有帮助,如果您需要更多信息,请询问:)

编辑:更改的代码 - 它仍然无法识别变量 City

这里是显示控制器正在传递它的 var_dump:

array(1) {
  [0]=>
  array(3) {
    ["Deal"]=>
    array(15) {
      ["id"]=>
      string(1) "2"
      ["... this part of is fine so ill hide this part of the var_dump
    }
    ["City"]=>
    array(2) {
      ["id"]=>
      string(1) "1"
      ["city"]=>
      string(7) "Glasgow"
    }
  }
}

你可以看到 $city['City']['city'] 正在被传递。 PS我将更改此字段的名称;)

谢谢 戴夫

【问题讨论】:

  • HAHAHAHA 喜欢 ufos?^^ 你可能是说“未定义”...
  • 你能发布 var_dump($deals) 的输出吗?
  • 对于它的价值,您可能需要考虑遵循模型的命名约定。如果您有一个名为 citys 的表,而不是使用 city 作为字段,您应该使用 name。然后您可以查询 City.name 而不是 City.city。

标签: php cakephp error-handling


【解决方案1】:

如果你使用 CakePHP,你应该按照它的方式来检索数据。将您的代码更改为:

function by_city($CityID = null)
{
    $this->paginate = array(
      'conditions' => array('City.id' => $CityID),
      'order' => array('Deal.start'=>'desc'),
      'limit' => 3
    );
    $deals = $this->paginate();
    $city = $this->City->find('first',array('conditions'=>array('City.id'=>$CityID)));
    $this->set(compact('deals','city'));
}

编辑:我知道你现在想做什么,检查上面的代码;)

【讨论】:

  • 假设递归 >= 0 - 否则最好在此处使用可包含
  • 他好像对蛋糕不熟悉,所以我先给他看基本的东西。但是,是的,Containable 肯定更好。
  • 是的 :) 当然。不过,我还是会笑出声来。让我们称它们为 UFI - 不明飞行指数^^ 没有冒犯,兄弟。
  • 只是为了澄清我想要的只是城市表中的城市名称,数组的其余部分来自交易表,这仍然是前进的方向吗? :)
  • 是的,它应该包含您需要的所有数据。你可以调试($deals);在视图中检查内容。它可以再优化一点,但在您了解 Containable 之前,我们就先这样吧。
猜你喜欢
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2013-11-23
  • 2018-08-29
  • 1970-01-01
  • 2018-01-09
相关资源
最近更新 更多