Cakephp 将同一模型连接到两个表上

Cakephp 将同一模型连接到两个表上,cakephp,associations,models,Cakephp,Associations,Models,我有一个关于模特协会的问题。考虑下面的应用程序:每个月向用户询问一个新问题。这些用户可以就这个问题给出提示,其他用户也可以对此提示做出反应。我的模型设置如下所示: Users hasMany Tips, Reactions Tips belongsTo User Tips hasMany Reactions Reactions belongsTo User, Tip 现在我想从所有用户那里得到关于这个问题的所有提示。我在tips表上做了一个简单的find all(查找所有)操作,返回所有

我有一个关于模特协会的问题。考虑下面的应用程序:每个月向用户询问一个新问题。这些用户可以就这个问题给出提示,其他用户也可以对此提示做出反应。我的模型设置如下所示:

Users hasMany Tips, Reactions

Tips belongsTo User

Tips hasMany Reactions

Reactions belongsTo User, Tip
现在我想从所有用户那里得到关于这个问题的所有提示。我在tips表上做了一个简单的find all(查找所有)操作,返回所有与用户和反应相关的提示。然而,反应并不包含用户,这显然非常重要

Cake会自动将我的find all转换为两个查询:一个在tips表上连接用户,另一个在reactions表上,但在users表上没有连接,这正是我所需要的

所以我的问题是:为什么用户表没有加入反应,我如何解决这个问题

代码:

用户模型:

public $hasMany = array('Tip', 'Reaction');
提示模式:

public $hasMany = array('Reaction' => array(
        'foreignKey' => 'tip_id'
    ));

public $hasOne = array('User' => array(
        'fields' => array(
            'User.id, User.first_name, User.last_name'
        ),
        'foreignKey' => 'id'
    ));
反应模型:

public $belongsTo = array('Tip', 'ReactionUser' => array(
        'className' => 'User',
        'foreignKey' => 'id'
    ));
控制器:

$tips = $this->Tip->find('all');
输出:

array(
    (int) 0 => array(
        'Tip' => array(
            'id' => '1',
            'user_id' => '1',
            'question_id' => '1',
            'tip' => 'Testing da new datavbase',
            'votes' => '0',
            'approved' => '1',
            'pic' => '',
            'created' => '2012-05-18 09:22:56'
        ),
        'User' => array(
            'id' => '1',
            'first_name' => 'Bart',
            'last_name' => 'De Bock'
        ),
        'Reaction' => array(
            (int) 0 => array(
                'id' => '1',
                'tip_id' => '1',
                'user_id' => '1',
                'reaction' => 'lorem ipsum',
                'votes' => '0',
                'approved' => '1',
                'pic' => '',
                'created' => '0000-00-00 00:00:00'
            )
        )
    ),
    (int) 1 => array(
        'Tip' => array(
            'id' => '2',
            'user_id' => '2',
            'question_id' => '1',
            'tip' => 'Test',
            'votes' => '0',
            'approved' => '1',
            'pic' => '',
            'created' => '2012-05-18 10:22:51'
        ),
        'User' => array(
            'id' => '2',
            'first_name' => 'Deborah',
            'last_name' => 'Rochtus'
        ),
        'Reaction' => array(
            (int) 0 => array(
                'id' => '2',
                'tip_id' => '2',
                'user_id' => '3',
                'reaction' => 'Say whaat?',
                'votes' => '2',
                'approved' => '1',
                'pic' => '',
                'created' => '2012-05-18 10:33:32'
            ),
            (int) 1 => array(
                'id' => '3',
                'tip_id' => '2',
                'user_id' => '4',
                'reaction' => 'Hmmm..',
                'votes' => '0',
                'approved' => '1',
                'pic' => '',
                'created' => '2012-05-18 10:33:44'
            )
        )
    )
)
如您所见,这些反应不包含关联的用户

谢谢你的帮助

使用可容纳的:

在AppModel中:

public$uses=array('Containable')

将查找呼叫更改为:

$this->Tip->find('all',array('contain'=>array('User','Reaction'=>array('User')))

请尝试此选项

$tips=$this->Tip->find('all'
排列(
“递归”=>“2”
)
);


递归值可能是1、2、3、4等,如您需要。

显示您的代码,,,您尝试了什么?请显示控制器的完整代码。。。。你的模型看起来很好。打印($tips);它显示的内容…在模态中,基本上我们必须这样定义:var$belongsTo=array('UserProduct'=>array('className'=>'UserProduct','foreignKey'=>'user\u product\u id','conditions'=>'','fields'=>'','order'=>'');