-2

I have a table for users, a table for cities and a table for states. Each user has a city, identified by city_id in its columns, and each city is related to a state by state_id key.

I want to get a list of users in each state, but I don't know how to access the user list using the city and the state.

All of them have, of course, their models, views and controllers (generated by bake). I didn't make any changes after bake generated them, except for users, whose password is salted and hashed before inclusion.

CakePHP is the latest (2.2.4), I'm using an AMP stack installed by myself and correctly configured.

tereško
  • 58,060
  • 25
  • 98
  • 150
ranieri
  • 2,030
  • 2
  • 21
  • 39
  • I'm not sure why this question was closed. It's a common problem to not understand how to get related data via associations, and the question made enough sense to answer it. – Dave Jan 02 '13 at 04:54

1 Answers1

2

Use CakePHP's Containable Behavior. Your code should then look something like this:

$this->State->find('all', array(
    'contain' => array(
        'City' => array(
            'User'
        )
    )
));

Then, your data will come back something like this (off top of my head):

>State
  >0
    id
    name
    >City
      >0
        id
        name
          >User
            >0
              id
              name
              email
            >1
              id
              name
              email
       >1
         id
         ...etc
Dave
  • 28,833
  • 23
  • 113
  • 183
  • I couldn't understand which one of them should be a containable. I found the documentation to be somewhat confusing, lol. Will update if any progress is made. – ranieri Jan 01 '13 at 14:34
  • Just set `public $actsAs = array('Containable');` in your AppModel and have it available for all of them. – Dave Jan 01 '13 at 20:11