从多个表中获取数据Cakephp

时间:2017-03-01 07:07:29

标签: php mysql cakephp

你好,我有四个表,我试图从所有这些表中获取数据。 表是

  1. 用户
  2. 的UserInfo
  3. 类别
  4. UserCategory
  5. 由于用户可以选择多个类别,因此我设置了一个名为UserCategory的单独表格。此表包含user_idcat_id作为userCategory表字段的外键

    Category表格中,字段为cat_name

    我想获取与特定用户ID关联的所有四个表中的所有数据。所以目前我已经实现了这一目标。我会告诉你如何。

    class UserInfo extends AppModel
    {
        public $useTable = 'user_info';
        public $primaryKey = 'user_id';
    
    
        public $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id',
                'type' => 'RIGHT',
                'fields' => array('User.user_id','User.email','User.active')
            )
    
    
        );
        public $hasMany = array(
            'UserCategory' => array(
                'className' => 'UserCategory',
                'foreignKey'    => 'user_id',
                'conditions' => array('user_id = UserCategory.user_id')
                //'order' => 'UserCategory. DESC'
            ),
    
    
        );
        public function getUserDetails($user_id){
            return $this->find('all', array(
                'conditions' => array(
                    'UserInfo.user_id' => $user_id
                ),
    
    
            ));
    
        }
    

    通过这样做

    $userDetails = $this->UserInfo->getUserDetails($user_id);
    

    我得到了这样的结果

    Array
    (
        [0] => Array
            (
                [UserInfo] => Array
                    (
                        [user_id] => 9
                        [first_name] => lorem
                        [last_name] => ipsum
                        [phone_no] => 123
                        [profile_img] => app/webroot/uploads/9/589c538daa276test.png.png
                        [registration_date] => 2017-02-09 08:33:33
                        [device_token] => ddd222
                    )
    
                [User] => Array
                    (
                        [user_id] => 9
                        [email] => new email
                        [active] => 1
                    )
    
                [UserCategory] => Array
                    (
                        [0] => Array
                            (
                                [user_category_id] => 1
                                [user_id] => 9
                                [cat_id] => 1
                            )
    
                        [1] => Array
                            (
                                [user_category_id] => 3
                                [user_id] => 9
                                [cat_id] => 1
                            )
    
                        [2] => Array
                            (
                                [user_category_id] => 4
                                [user_id] => 9
                                [cat_id] => 2
                            )
    
                    )
    
            )
    
    )
    

    我想知道如何在上面的数组中从类别表中获取类别名称。

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用containable

public function getUserDetails($user_id) {
    $this->Behaviors->attach('Containable');
    return $this->find('all', array(
        'conditions' => array(
            'UserInfo.user_id' => $user_id
        ),
        'contain' => array(
            'User', 'UserCategory.Category'
        )
    ));
}