Cakephp Unidentified Index问题

时间:2011-08-02 00:32:35

标签: php cakephp error-handling

你一直试图让这个工作多年,

我有交易控制器

<?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我将更改此字段的名称;)

由于 戴夫

1 个答案:

答案 0 :(得分:0)

如果您使用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'));
}

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