dongyuanliao6204 2017-03-01 07:07
浏览 25
已采纳

从多个表Cakephp中获取数据

hello I have four tables and I am trying to fetch data from all of them. Tables are

  1. User
  2. UserInfo
  3. Category
  4. UserCategory

Since user can select multiple categories so I have setup a separate table called UserCategory. This table has user_id and cat_id as foreign key of user and Category Table fields

In Category Table, the field is cat_name

I want to get All the data from all the four tables associated with the particular userID. So at the moment I have achieved this. I'll show you how.

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


        ));

    }

By doing this

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

I get a result like this

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
                        )

                )

        )

)

I want to know how Can I get Category names from category table in the above array.

  • 写回答

1条回答 默认 最新

  • dsnw2651 2017-03-01 08:28
    关注

    You can use containable in this case:

    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'
            )
        ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP