2012-02-08 66 views
1

在我的「CouponsController.php」,我有以下兩個功能:CakePHP的:使用相同的佈局

public function index() { 
    $this->set('coupons', $this->Coupon->find('all')); 
} 

public function restaurants() { 
    $this->set('coupons', $this->Coupon->findBycategory_id('1')); 
    $this->render("index"); 
} 

基本上,我想指數函數返回所有的優惠券和餐廳只返回類別1(但我想使用相同的視圖文件)。

我最終得到這個errror:

Notice (8): Undefined index: Coupon [APP/View/Coupons/index.ctp, line 16] 

這是因爲數組如何返回爲他們每個人。這是我的看法文件和結果的每一頁:

Coupons/index.ctp: 
foreach ($coupons as $c) { 
    print_r($c); 
} 

INDEX function: 
Array ([Coupon] => Array ([id] => 1 [vendor_id] => 1 [category_id] => 1 [title] => $10 For Whatever [price] => 10.00 [value] => 20.00 [start_at] => 2012-02-07 12:03:00 [end_at] => 2012-02-29 12:03:05 [details] => Test [terms] => Test [mini_views] => 0 [large_views] => 0 [created] => 2012-02-08 12:03:12)) Array ([Coupon] => Array ([id] => 2 [vendor_id] => 2 [category_id] => 2 [title] => Test [price] => 100.00 [value] => 200.00 [start_at] => 0000-00-00 00:00:00 [end_at] => 0000-00-00 00:00:00 [details] => [terms] => [mini_views] => 0 [large_views] => 0 [created] => 2012-02-08 12:14:03)) 

RESTAURANTS function: 
Array ([id] => 1 [vendor_id] => 1 [category_id] => 1 [title] => $10 For Whatever [price] => 10.00 [value] => 20.00 [start_at] => 2012-02-07 12:03:00 [end_at] => 2012-02-29 12:03:05 [details] => Test [terms] => Test [mini_views] => 0 [large_views] => 0 [created] => 2012-02-08 12:03:12) 
+0

我已經想通了..我在餐館需要「findALLBY」() – execv 2012-02-08 19:31:21

回答

1

那麼它是如何CakePHP的回報吧,週轉會

foreach ($coupons as $coupon) { 
    $c = $coupon; 
    if (isset($coupon["Coupon"])) { // if is set index in array ["Coupon"] { 
     $c = $coupon["Coupon"]; 
    } 
    print_r($c); 
} 

public function restaurants() { 
    $params = array('conditions' => array('Coupon.category_id' => 1)); 

    $this->set('coupons', $this->Coupon->find('all', $params)); 
    $this->render("index"); 
}