我在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)));我想返回User和UserProfile,但它再次返回所有关联。
请帮帮忙。
发布于 2014-03-31 14:23:45
将此代码添加到您的AppModel.php
public $actsAs = array('Containable');并设置与UserProfile的关联。我猜这应该是$hasOne关系..
发布于 2014-04-09 13:47:52
你也可以在运行中加载“Containable”
$this->User->Behaviors->load('Containable');
$this->User->find('first', array('contain' => 'UserProfile','conditions' => array('User.id' => 15)));https://stackoverflow.com/questions/22749540
复制相似问题