【问题标题】:Fetch data from multiple tables Cakephp从多个表中获取数据 Cakephp
【发布时间】:2017-03-01 07:07:29
【问题描述】:

您好,我有四个表,我正在尝试从所有表中获取数据。 表是

  1. 用户
  2. 用户信息
  3. 类别
  4. 用户类别

由于用户可以选择多个类别,因此我设置了一个名为 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
                        )

                )

        )

)

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

【问题讨论】:

    标签: php mysql cakephp


    【解决方案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'
            )
        ));
    }
    

    【讨论】:

    • 很高兴为您提供帮助 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多