I have 2 controllers: “CouponsController” and “CategoriesController”. Here is the model for each:
class Category extends AppModel {
public $hasMany = array('Coupon' => array('className' => 'Coupon',
'foreignKey' => 'category_id')
);
}
class Coupon extends AppModel {
public $belongsTo = array('Category' => array('className' => 'Category',
'foreignKey' => 'category_id')
);
}
I want to create links to view the coupons in each category. What I ended up coming up with was the following for the CouponsController (this example is for restaurants):
public function restaurants() {
$this->set('coupons', $this->Coupon->findAllBycategory_id('1'));
$this->render("index");
}
I have 2 questions:
1: Is there a better way to display all of the posts from each category (right now, I’m just copying the function above for “hotels” and changing the category id. I have it render the same view each time).
2: Is there a better way to access the coupons for a given category (more OOP: ie: Coupon->Category etc) than the way I am doing it?
I would allow restaurants() to take a category variable. If none is set, then it displays all coupons in all categories. If category is set, it searches by id like you have. Then I’d have links iterating through the categories with links back to the same page with the variable entered.
Controller:
View:
Something along these lines.