2012-02-08 64 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"); 
}