我有一个调用的链接:
<a href="/coupons/print_coupon/<?php echo $coupon['id']; } ?>" id="print"><span style="margin-left:24px;">Print</span></a>
它插入正确的ID,我的路由将其发送到正确的视图文件。我有一个debug命令来显示打印页面的整个数组,但我得到一个空数组。这是我的控制器代码:
<?php
class CouponsController extends AppController {
public $name='Coupons';
public $uses=array('User', 'Coupon', 'Restaurant');
public $layout='pagelayout';
public function print_coupon($name=null) {
$this->set('title', 'Print your coupon');
$f=$this->Coupon->find('first', array('conditions'=>array('Coupon.id'=>$this->params['id'])));
$this->set('name', $f);
}
} ?&GT;
这是我的优惠券型号:
<?php
class Coupon extends AppModel {
public $name='Coupon';
var $belongsTo=array(
'Restaurant'=>array (
'className'=>'Restaurant',
'foreignKey'=>'restaurant_id'
)
);
}
&GT;
这是我的餐厅模特:
<?php
class Restaurant extends AppModel {
public $name='Restaurant';
var $hasMany=array(
'Coupon'=>array(
'className'=>'Coupon',
'foreignKey'=>'restaurant_id'
)
);
var $belongsTo=array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user'
)
);
}
?>
我尝试使用
进行变体操作 <a href="/coupons/print_coupon/<?php echo $res['Restaurant']['coupon']; } ?>" id="print"><span style="margin-left:24px;">Print</span></a>
以及我的控制器:
public function print_coupon($name=null) {
$this->set('title', 'Print your coupon');
$f=$this->Coupon->find('first', array('conditions'=>array('Coupon.id'=>$this->params['id'])));
$this->set('name', $f);
}
以及其他几个,每当我调试($ name)时,我得到一个空数组。我还没有将优惠券与我的其他模型联系起来用于其他任务,但我认为我的餐厅模型可能有问题。
作为参考,我有这些是彼此相同的: Restaurants.coupon = Coupon.id Coupon.restaurant_id = Restaurant.id
答案 0 :(得分:1)
你张贴的第一个锚码有一个不应该在那里的花括号。当你使用它时:
<a href="/coupons/print_coupon/<?php echo $coupon['id']; ?>" id="print"><span style="margin-left:24px;">Print</span></a>
它应该产生一些这样的网址:
/coupons/print_coupon/75
然后在你的CouponsController中,75变成了print_coupon参数中的$name
(使用默认路由设置),我建议将其更改为$coupon_id
,因此你的函数应如下所示:
public function print_coupon($coupon_id) {
$this->set('title', 'Print your coupon');
$f=$this->Coupon->find('first', array('conditions'=>array('Coupon.id'=>$coupon_id)));
$this->set('name', $f);
}
现在应该可以在您的视图中访问$name
了。使用上面的代码,控制器中将不存在$name
。
我认为Cake 2中不存在$this->params
。*。 debug($this->request->params);
但你可以从控制器中获取参数。