0

我有以下表格

文章


ID 标题 描述 图片 作者 发布 创建

注释


id user_id article_id 文本创建

用户


id登录名 电子邮件 通行证

我希望文章评论相关,评论用户相关,但我不能。然而我有以下模型。

文章

<?php

/**
* Model de artigos
*/
class Article extends AppModel {

    public $name = 'Article';

    public $hasMany = array(
                            'Comment' => array(
                                                'className' => 'Comment',
                                                'dependent' => false,
                                                'fields' => array('Comment.user_id','Comment.created','Comment.text'),
                                                'foreignkey' => 'article_id',

                                                )
                            );

    /**
    * Regras de validação
    *
    * @var array
    */
    public $validate = array();

    /**
    * Lista dos artigos mais recentes
    *
    * @param int $limit Quantidade de artigos
    * @param array $params Parâmetros extras de busca
    *
    * @return array
    */
    public function recent($limit = 5, $params = array()) {
        $params = Hash::merge(array(
        'conditions' => array('Article.published' => true),
        'order' => array('Article.created' => 'DESC'),
        'limit' => $limit
        ), $params);

        return $this->find('all', $params);
    }

}

评论

<?php

/**
* Model de comentários
*/
class Comment extends AppModel {

    public $name = 'Comment';

    public $belongsTo = array(
                            'User' => array(
                                            'className' => 'User',
                                            'foreignKey'   => 'user_id',
                                            'type'  => 'left'
                                            )
                             );

    /**
    * Regras de validação
    *
    * @var array
    */
    public $validate = array();

    /**
    * Lista dos comentários mais recentes
    *
    * @param int $limit Quantidade de comentários
    * @param array $params Parâmetros extras de busca
    *
    * @return array
    */
    public function recent($limit = 5, $params = array()) {
        $params = Hash::merge(array(
            'conditions' => array('Comment.published' => true),
            'order' => array('Comment.created' => 'DESC'),
            'limit' => $limit
        ), $params);

        return $this->find('all', $params);
    }

}

用户

/**
* Model de usuários
*/
class User extends AppModel {

    public $name = 'User';

    /**
    * Regras de validação
    *
    * @var array
    */
    public $validate = array();

}

我只能将文章评论联系起来,但我不能将评论用户联系起来。我需要做什么?感谢您的帮助。

4

1 回答 1

1

您需要添加$hasMany到用户模型。

/**
* Model de usuários
*/
class User extends AppModel {

    public $name = 'User';
    public $hasMany = array(
        'Comment' => array(
            'className' => 'Comment',
            'dependent' => false,
            'fields' => array('Comment.user_id','Comment.created','Comment.text'),
            'foreignkey' => 'user_id',
        )
    );
    /**
    * Regras de validação
    *
    * @var array
    */
    public $validate = array();

}
于 2013-02-12T14:55:38.953 回答