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 722523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:00:00+00:00 2026-05-14T06:00:00+00:00

Following the intro cakephp tutorial to create a blog, I created a database to

  • 0

Following the intro cakephp tutorial to create a blog, I created a database to view, add/edit/delete businesses. Each business has a state or province associated with it, and I’m wondering how I would go about to generate a page that lists all the States, like /states and a page would be like /states/california and lists all the businesses in california.

Right now I currently just have one page that lists all businesses. Wondering how I would design the model/controller/view and routes to handle this. Can’t really find a source online that elaborates on this or I just don’t know how to look.

  • 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-14T06:00:01+00:00Added an answer on May 14, 2026 at 6:00 am

    Since you’re new to cake,I think it’s not a good idea to start from HABTM relationship which is what you need in your question and also the most complicated one.So I assume your models relationship as a easier one :hasMany.E.g as follows:

    /*this is  your state model
     *state table in database should like:id,name
     *and business table in database:id,name,state_id
     */
    class State extends AppModel
    {
        var $name = 'State';
        var $hasMany = array(
            'Business'=>array(
                    'className'=>'Business',
                    'foreignKey'=>'state_id',
                    )
    );   //this means a State can hasMany Businesses while a Business only belongs to one State
    
    }
    

    Then make the action in your state controller as this:

     /*in your state controller*/
     function showBusinessesByState($statename)
     {
         if($statename && $thestate = $this->State->findByName($statename))
         {
             $this->set('state',$thestate);//debug $thestate you'll find data you need
         }
         else
         {
             $this->Session->setFlash("something wrong happens!");
         }
     }
    

    Now I think you may handle the view file yourself,with a $state variable to retrieve the data $thestate which also contains a businesses list in it.

    Now in the /app/config/routes.php do the routing part:

    Router::connect('/states/*',array('controller'=>'states','action' => 'showBusinessesByState'));
    

    After finish that you may get what you need with /states/somestate.When you go through this,you may try to solve this with the best way —hasAndBelongsToMany.

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

Sidebar

Related Questions

Ok, in CakePHP, I have the following code in my view add.ctp : echo
I've just been following the tutorial on the CakePHP website to create a simple
I'm following the blog tutorial on CakePHP website, but validation doesn't work and I
I'm following the Auth tutorial and in this stage http://book.cakephp.org/view/1547/Acts-As-a-Requester aros table is not
I’ve been following the e-commerce tutorial located here: http://netbeans.org/kb/docs/javaee/ecommerce/intro.html Code repo of project here
I've been following a tutorial for implementing auto-complete functionality on my CakePHP 1.3 application.
Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
I made a blog database and created some tables in it. I have wriTten
I've been following the django tutorial over at https://docs.djangoproject.com/en/dev/intro/tutorial03/ but I've hit a brick
I am following the Intro to rails Screencast - the tutorial I wish I

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.