0

I have a problem with CakePHP. I tested many ways, but can't find a solution for it. I have these tables:

users

id
name
country_code

countries

code
title

skills

code
title

roles

id
title

events

id
title
country_code

events_users

event_id
user_id
role_id
skill_code

events hasAndBelongsToMany users and events_users is my join table, and when I read data, it gives me something like this:

array(
    (int) 0 => array(
        'Event' => array(
            'id' => '1',
            'title' => '40th WorldSkills Competitions',
            'year' => '2011',
            'country_code' => 'LV',
            'created' => '2013-02-08 00:00:00'
        ),
        'User' => array(
            (int) 0 => array(
                'password' => '*****',
                'id' => '14',
                'name' => 'test',
                'family' => 'testian',
                'username' => 'test@mail.me',
                'country_code' => 'BR',
                'telphone' => '465465',
                'avatar' => 'avatar_51299c1da268f20110912043238_rodekamp_04.jpg',
                'address' => 'AA',
                'admin' => '0',
                'created' => '2013-02-24 05:50:37',
                'modified' => '2013-02-24 05:50:37',
                'EventsUser' => array(
                    'id' => '1',
                    'user_id' => '14',
                    'event_id' => '1',
                    'role_id' => '1',
                    'skill_code' => '17',
                    'manage' => '100'
                )
            ),
            (int) 1 => array(
                'password' => '*****',
                'id' => '5',
                'name' => 'John',
                'family' => 'Smith',
                'username' => 'john@me.com',
                'country_code' => 'IE',
                'telphone' => '147',
                'avatar' => '20120504124040_franziska_peter_ok.jpg',
                'address' => 'No 55',
                'admin' => '0',
                'created' => '2013-02-10 15:24:13',
                'modified' => '2013-02-10 15:28:50',
                'EventsUser' => array(
                    'id' => '2',
                    'user_id' => '5',
                    'event_id' => '1',
                    'role_id' => '2',
                    'skill_code' => '17',
                    'manage' => '0'
                )
            ),
            (int) 2 => array(
                'password' => '*****',
                'id' => '4',
                'name' => 'hadi',
                'family' => 'mm',
                'username' => 'design.zerzem@gmail.com',
                'country_code' => 'AE',
                'telphone' => '415456',
                'avatar' => '',
                'address' => 'sadjfklsaf asd f',
                'admin' => '0',
                'created' => '2013-02-10 09:01:28',
                'modified' => '2013-02-24 06:43:42',
                'EventsUser' => array(
                    'id' => '3',
                    'user_id' => '4',
                    'event_id' => '1',
                    'role_id' => '4',
                    'skill_code' => '17',
                    'manage' => '0'
                )
            )
)

It's ok, but with events_users I want get data about skill and role that are in events_users (skill_code and role_id). How should I create models to give me all this data?

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Hadi Mostafapour
  • 1,994
  • 2
  • 13
  • 21

1 Answers1

2

I think reading about "hasMany Through" should answer your question.

Basically, you make a model for your join table, then you can associate it like any other model.

When you go to retrieve your data, use CakePHP's Containable behavior (if you haven't used it before, be prepared to get addicted to it).

Dave
  • 28,833
  • 23
  • 113
  • 183