I have a link that calls:
<a href="/coupons/print_coupon/<?php echo $coupon['id']; } ?>" id="print"><span style="margin-left:24px;">Print</span></a>
It plugs in the correct id, and my route sends it to the correct view file. I have a debug command to show the entire array for the print page, but I’m getting an empty array. Here is my controller code:
<?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);
}
}
?>
Here is my Coupon Model:
<?php
class Coupon extends AppModel {
public $name='Coupon';
var $belongsTo=array(
'Restaurant'=>array (
'className'=>'Restaurant',
'foreignKey'=>'restaurant_id'
)
);
}
?>
and here is my Restaurant Model:
<?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'
)
);
}
?>
I have tried variations using
<a href="/coupons/print_coupon/<?php echo $res['Restaurant']['coupon']; } ?>" id="print"><span style="margin-left:24px;">Print</span></a>
along with in my controller:
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);
}
as well as a few others, and whenever I debug($name) I get an empty array. I have had no problems associating Coupon with my other models for other tasks yet, but i think something may be wrong with my Restaurant Model.
For reference, I have these are equal to each other:
Restaurants.coupon = Coupon.id
Coupon.restaurant_id=Restaurant.id
The first anchor code you posted has a curly brace that shouldn’t be there. When you use this:
It should yield some url that’s like this:
Then in your CouponsController that 75 turns into the
$namein your print_coupon parameters (with default routing settings), I recommend changing that to$coupon_id, so your function should look like this:Now
$nameshould be accessible in your view. With this code above,$namewon’t exist within the controller.I don’t think
$this->paramsexists in Cake 2.*.debug($this->request->params);You can get the params from there in the controller, though.