我如何在manyToMany关联中获取用户数据

时间:2013-03-10 15:20:16

标签: cakephp cakephp-2.0 cakephp-appmodel

我遇到了CakePHP的问题。我测试了很多方法,但无法找到解决方案。 我有这些表:

用户

id
name
country_code

国家

code
title

技能

code
title

角色

id
title

事件

id
title
country_code

events_users

event_id
user_id
role_id
skill_code

事件hasAndBelongsToMany用户和events_users是我的联接表,当我读取数据时,它给我这样的东西:

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'
                )
            )
)

没关系,但是对于events_users,我想要获取有关events_users(skill_code和role_id)中的技能和角色的数据。我应该如何创建模型来提供所有这些数据?

1 个答案:

答案 0 :(得分:2)

我认为阅读"hasMany Through"应该回答你的问题。

基本上,您为连接表创建模型,然后您可以像任何其他模型一样关联它。

当您去检索数据时,请使用CakePHP's Containable behavior(如果您以前没有使用过它,请准备好上瘾)。