dongshan8953 2017-03-01 10:30
浏览 23
已采纳

HasMany Multiple选择不工作的Cakephp

Hello I am trying to select data from multiple tables in HasMany but its not working. Here is my code

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',
            'dependent' => true,
            'conditions' => array('user_id = UserCategory.user_id')
            //'order' => 'UserCategory. DESC'

        )


);



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

            )

        ));

    }

If I try to fetch the data from another table which has user_id in it like this

public $hasMany = array(
            'UserCategory' => array(
                'className' => 'UserCategory',
                'foreignKey'    => 'user_id',
                'dependent' => true,
                'conditions' => array('user_id = UserCategory.user_id')
                //'order' => 'UserCategory. DESC'

            ),

            'Skill' => array(
                'className' => 'Skill',
                'foreignKey'    => 'user_id',
                'dependent' => true


            )
    );

It doesn't fetch. When I run sql dump. It doesn't run query at all. So Meaning I am unable to get the data from Skills Table

  • 写回答

1条回答 默认 最新

  • douqie6454 2017-03-01 10:55
    关注

    I suggest you to read the docs thoroughly for containable in cakephp 2. In summary, when you use containable like :

    'contain' => array(
        'User', 'UserCategory.Category',
    )
    

    You can add your desired Model in it like in your case this will help you :

    'contain' => array(
        'User', 'UserCategory.Category', 'Skill'
    )
    

    Containable helps to filter your find() data as per your requirement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊