Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:47:10+00:00 2026-05-30T01:47:10+00:00

I have 2 controllers: CouponsController and CategoriesController. Here is the model for each: class

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:47:11+00:00Added an answer on May 30, 2026 at 1:47 am

    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:

    public function restaurants(category_id = null) {
      $this->set('categories', $this->Category->find('all');
      if(isset($category_id)) {
        $this->set('coupons', $this->Coupon->findAllBycategory_id($category_id));
      } else {
        $this->set('coupons', $this->Coupon->find('all'));
      }
      $this->render("index"); 
    }
    

    View:

    //Some menu somewhere
    <ul>
    <?php foreach($categories as $category): ?>
      <li><?php $this->Html->link($category['Category']['name'], ('action'=>'restaurants', $category['Category']['id']) ?> </li>
    <?php endforeach; ?>   
    </ul>
    

    Something along these lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using CakePHP 2.0. I have 2 models/controllers (Category/Coupon): // File: Model/Coupon.php // Table
I have 2 controllers that depend on ISomeService public class FirstController { public FirstController(ISomeService
I have my controllers with the following extensions: Controller_Login extends Controller_Layout Controller_Layout extends Controller_Template
I have two controllers, orders and tasks - each order has many tasks. In
I have 2 controllers: articles and comments. For each articles I need to display
I have a controller called 'admin' class Admin extends CI_Controller { and under the
I have controllers that work in context of some model. Example: controllers UsersController and
I have MVC3 controllers in external assemblies using MVC contrib's portable areas but I'm
I have two controllers which share most of their code (but must be, nonetheless,
i have several controllers which will all be using common functionality. So i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.