在提交的注释表上加入用户表时,模型关联处于关闭状态

时间:2013-01-08 09:53:10

标签: mysql cakephp model associations cakephp-2.0

我在CakePHP中的关联有点过时了。这是问题所在。我有一个users表 和comments表。检索提交时,我想加入users表 使用Comment.user_id == User.id。{/ p>在comments表上

评论模型:

class Comment extends AppModel {
  var $name = 'Comment';
  var $belongsTo = array('User');

  var $hasMany = array(
    'CommentsLike' => array(
      'className' => 'CommentsLike'
    ),
    'Comment' => array(
      'className' => 'Comment'
    )
  );

  var $hasAndBelongsToMany = array(
    'User' => array(
      'className' => 'User',
      'joinTable' => 'comments',
      'foreignKey' => 'Comment.user_id',
      'associationForeignKey' => 'user_id'
    ),
    'Submission' => array(
      'className' => 'Submission'
    )
  );

用户模型:

class User extends AppModel {
  var $name = 'User'; 
  var $hasMany = array('Comment');

  var $hasAndBelongsToMany = array(
    'Comment' => array(
      'className' => 'Comment',
      'joinTable' => 'comments',
      'foreignKey' => 'Comment.user_id',
      'associationForeignKey' => 'user_id',
    ),  
    'Submission' => array(
      'className' => 'Submission',
      'order' => 'Submission.created DESC'
    )   
  );

当我抓住submission时,它抓取所有评论,但它没有加入 我想要的users

提交模式:

class Submission extends AppModel {
    var $name = 'Submission';
    var $belongsTo = array(
      'User' => array(
        'className' => 'User'
      )   
    );  

    var $hasMany = array(
      'Comment' => array(
        'className' => 'Comment',
        'order' => 'Comment.created DESC',
        'conditions' => array(
          'Comment.deleted' => 0
        )   
      ),  
      'SubmissionsVote' => array(
        'className' => 'SubmissionsVote'
      ),  
      'SubmissionThumbnails' => array(
        'className' => 'SubmissionThumbnails'
      )   
    );

提交中的查找将返回此信息:

[Submission] => Array
    (
        // removed for sake of stackoverflow length
    )

[User] => Array
    (
        [id] => 2
        [username] => bob_cobb
    )

[Comment] => Array
    (
        [0] => Array
            (
                [id] => 1
                [submission_id] => 112
                [comment] => this is a test comment
                [user_id] => 2
                [created] => 5 minutes ago
                [deleted] => 
                [parent_id] => 
            )

    )

正如您所看到的,[Comment][0]也应该有[Comment][0][User]或类似内容。

为什么我要这个?简单:我想抓住那个人的用户名 由comments表中的user_id的外键注释。我已经阅读了文档和教程,并且非常确定我对此很接近,但不确定是什么阻碍了我。

以下是comments表的屏幕截图: comments table

以下是users表结构的屏幕截图: users table

1 个答案:

答案 0 :(得分:0)

您可以选择以下几种方式:

将Submission模型中的递归属性设置为获取相关数据所需的级别(请参阅http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive

或(更可取)使用Containable行为(请参阅http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html)指定要返回的相关数据:

class Submission extends AppModel

    public $actsAs = array('Containable');

}

在你的发现中使用它:

$data = $this->Submission->find('all', array(
    'conditions'=>array(your conditions here),
    'contain'=>array(
        'User,
        'Comment'=>array(
            'User'
        )
    )
));

大多数人在每个模型中都使用可包含的内容,因此将其添加到AppModel并将AppModel :: recursive设置为-1