1

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

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

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

$this->User->find('first', array('conditions' => array('User.id' => 15)));

$this->User->find('first', array('contain' => 'UserProfile','conditions' => array('User.id' => 15)));

我想返回用户和用户配置文件,但它再次返回所有关联。

请帮忙。

4

2 回答 2

3

将此添加到您的 AppModel.php

public $actsAs = array('Containable');

并设置与 UserProfile 的关联。我想这将是 $hasOne 关系..

于 2014-03-31T06:23:44.927 回答
2

您还可以即时加载“可包含”

$this->User->Behaviors->load('Containable');
$this->User->find('first', array('contain' => 'UserProfile','conditions' => array('User.id' => 15)));
于 2014-04-09T05:47:51.977 回答