Reputation: 1
I have the following Tables
Articles
Id title description image author published created
Comments
Id user_id article_id text created
Users
Id login name email pass
I want that Article relates with Comments and Comments relates with Users, but i can not. Yet I have the following models.
Article
<?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);
}
}
Comment
<?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);
}
}
User
/**
* Model de usuários
*/
class User extends AppModel {
public $name = 'User';
/**
* Regras de validação
*
* @var array
*/
public $validate = array();
}
I only can relates Articles with Comments, but I can not Comments with Users. What I need do? Thanks for helping.
Upvotes: 0
Views: 190
Reputation: 1289
You need to add the $hasMany
to user model.
/**
* 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();
}
Upvotes: 1