开发者

Cakephp Unidentified Index problem

Hi been trying to get this to work for ages,

i have a deals controller

<?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());

}

and the by_city view is :

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

Now when i run this i am getting a "unidentified index City" notice

when i run var_dump it says that theres an array City and 'city' has a value aswell which sounds to me like the controller has passed this

i'm lost now lol

开发者_C百科

All help appreciated, if you need more info just ask :)

Edit: Changed code - its still not identifying the variable City

here is the var_dump to show that the controller is passing it:

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"
    }
  }
}

as you can see $city['City']['city'] is being passed. PS i will change the name of this field ;)

Thanks Dave


If you use CakePHP, you should follow its way to retrieve data. Change your code to this:

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'));
}

Edit: I see what you want to do now, check the code above ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜