首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP containable不工作

CakePHP containable不工作
EN

Stack Overflow用户
提问于 2014-03-31 04:38:36
回答 2查看 2.5K关注 0票数 1

我在cakePHP中有一个与多个其他模型相关联的“用户”模型。

代码语言:javascript
复制
    public $belongsTo = array(
        'Reference' => array(
            'className' => 'Reference',
            'foreignKey' => 'reference_id',
        ),
        'SecurityQuestion' => array(
            'className' => 'SecurityQuestion',
            'foreignKey' => 'question_id',
        )
    );

    public $hasMany = array(
        'UserReview' => array(
            'className' => 'UserReview',
            'foreignKey' => 'user_id',
            'dependent' => true,
        ),
        'UserLicense' => array(
            'className' => 'UserLicense',
            'foreignKey' => 'user_id',
            'dependent' => true,
        ),
        'UserCertification' => array(
            'className' => 'UserCertification',
            'foreignKey' => 'user_id',
            'dependent' => true,
        ),
        'UserFavorite' => array(
            'className' => 'UserFavorite',
            'foreignKey' => 'user_id',
            'dependent' => true,
        ),
   }

下面的代码返回所有的模型。

代码语言:javascript
复制
$this->User->find('first', array('conditions' => array('User.id' => 15)));

代码语言:javascript
复制
$this->User->find('first', array('contain' => 'UserProfile','conditions' => array('User.id' => 15)));

我想返回User和UserProfile,但它再次返回所有关联。

请帮帮忙。

EN

回答 2

Stack Overflow用户

发布于 2014-03-31 14:23:45

将此代码添加到您的AppModel.php

代码语言:javascript
复制
public $actsAs = array('Containable');

并设置与UserProfile的关联。我猜这应该是$hasOne关系..

票数 3
EN

Stack Overflow用户

发布于 2014-04-09 13:47:52

你也可以在运行中加载“Containable”

代码语言:javascript
复制
$this->User->Behaviors->load('Containable');
$this->User->find('first', array('contain' => 'UserProfile','conditions' => array('User.id' => 15)));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22749540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档