tags:

views:

84

answers:

1

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.

A: 

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.

SpawnCxy
Thanks, just what I needed! I will try to tinker around this. Just wanted a push in the right direction. I'll read up on the database relationships.
xtine
Ok ran into a problem. I created my model/controller/view sucessfully, however when I try to view /state it gives me an error:Warning (512): SQL Error: 1054: Unknown column 'State.id' in 'field list' [CORE/cake/libs/model/datasources/dbo_source.php, line 549]Query: SELECT `State`.`state_id`, `State`.`state`, `State`.`state_abbr`, `State`.`id` FROM `states` AS `State` WHERE 1 = 1 I don't know why it's trying to find state.id as I set the foreign key to be state_id.
xtine
@xtine,Show me your `find` code in the controller,plz.
SpawnCxy
in the index function I wanted it to spit out everything, so I have $this->set('states', $this->State->find('all'));
xtine
@xtine,well,see http://codeviewer.org/view/code:daf
SpawnCxy
Thank you, that worked. I had to rename columns in my database tables for states (from state_id to id and state to name). I got my views to work through routes as well. =)
xtine
@xtine,thst's cool, good luck.:)
SpawnCxy