2011-08-02 41 views
0

喜一直試圖讓這對年齡工作,CakePHP的身份不明的指標問題

我有一個交易控制器

<?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它說,這是一個ARR AY市和「城市」有一個值藏漢像控制器已通過此

現在我失去了笑

所有幫助表示讚賞,如果你需要更多的信息只是問這聽起來對我:)

編輯:改變的代碼 - 它仍然沒有確定變量市

這裏是的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

HAHAHAHA像飛碟^^你可能意味着「不確定」? .. – mark

+0

你可以發佈var_dump($ deals)的輸出嗎? – Ehtesham

+1

對於它的價值,您可能需要考慮遵循模型的命名約定。如果你有一張名爲城市的表格,而不是使用城市作爲字段,則應該使用名稱。然後你可以查詢City.name而不是City.city。 –

回答

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

編輯:我看你現在想做,檢查上面的代碼是什麼;)

+0

假設遞歸是> = 0 - 否則最好在這裏使用可容忍的 – mark

+0

他似乎是蛋糕的新手,所以我只是先向他展示基本的東西。但是,Containable肯定更好。 –

+0

是啊:)當然。儘管如此,我還是笑了起來。讓我們打電話給他們UFI - 不明飛行指數^^沒有進攻,兄弟。 – mark