0

我在 CakePHP 中的联想有点不对劲。这是问题所在。我有一张users桌子和一张comments桌子。检索提交时,我想用 Comment.user_id == User.id加入users表上的表。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'
    )   
  );

当我抓住 asubmission时,它会抓住它的所有评论,但它没有加入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]或类似的东西。

我为什么要这个?简单:我想从表中获取通过 user_id 的外键评论的人的用户名comments。我已经阅读了文档和教程,并且很确定我对此很接近,但不确定是什么阻碍了我。

这是comments表格的屏幕截图: 评论表

这是users表结构的屏幕截图: 用户表

4

1 回答 1

0

你可以有几个选择:

将提交模型中的递归属性设置为您需要取回关联数据的级别(请参阅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

于 2013-01-08T10:26:15.017 回答