如何使用Cakephp对查询进行连接?
我有一个Comments表,用户可以在其中对另一个用户进行如下评论: 这些是制约因素: 使用此查询时:如何使用Cakephp对查询进行连接?,cakephp,Cakephp,我有一个Comments表,用户可以在其中对另一个用户进行如下评论: 这些是制约因素: 使用此查询时: $comments = TableRegistry::getTableLocator() ->get('Comments') ->find('all'); $query = $comments ->find('all') ->contain(['Users']); 它检索注释
$comments = TableRegistry::getTableLocator()
->get('Comments')
->find('all');
$query = $comments
->find('all')
->contain(['Users']);
它检索注释,但仅在注释id上应用连接。
虽然我想检索comment对象,其中包含它的两个相关用户,一个作为评论员,另一个作为已评论,;那么,如何构建查询呢
以下是评论:
class CommentsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('comments');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'commentator_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'commented_id',
'joinType' => 'INNER'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->scalar('content')
->maxLength('content', 255)
->requirePresence('content', 'create')
->notEmpty('content');
$validator
->integer('score')
->requirePresence('score', 'create')
->notEmpty('score');
$validator
->dateTime('created_at')
->requirePresence('created_at', 'create')
->notEmpty('created_at');
$validator
->dateTime('updated_at')
->requirePresence('updated_at', 'create')
->notEmpty('updated_at');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['commentator_id'], 'Users'));
$rules->add($rules->existsIn(['commented_id'], 'Users'));
return $rules;
}
}我不确定您的确切输出是什么,但根据我的理解,您可以使用自定义连接进行相同的操作:
$data = $this->Comments->find()
->join([
'Users' => [
'table' => 'users',
'type' => 'INNER',
'conditions' => [
'Users.id=Comments.commentator_id',
'OR' => [
'Users.id=Comments.commented_id'
]
]
]
]);
或者,您可以根据需要对此进行修改
希望这会有所帮助。将您的人际关系更改为以下内容:
$this->belongsTo('Commenters', [
'className' => 'Users',
'foreignKey' => 'commentator_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'commented_id',
'joinType' => 'INNER'
]);
$comment->commenter->name; // Name of the person who commented
$comment->user->name; // Name of the person who was commented on
因此,您可以包含这两种不同的关系
$comments = TableRegistry::getTableLocator()
->get('Comments')
->find('all')
->contain(['Commenters', 'Users']);
然后像这样访问它们:
$this->belongsTo('Commenters', [
'className' => 'Users',
'foreignKey' => 'commentator_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'commented_id',
'joinType' => 'INNER'
]);
$comment->commenter->name; // Name of the person who commented
$comment->user->name; // Name of the person who was commented on
请从评论表模型发布您的关联。也可以向用户发布一个。两者的表架构也不会有任何影响。@cnizzardini这里我添加了:)我认为这不重要,因为您指定了键,但约定是user_id而不是commentor_id,comment_id而不是commented_id。这里的命名约定通常是关闭的。为什么不只拥有一个Comments表、一个Users表和一个UserComments表,并使用标准的蛋糕约定呢。好的,谢谢你,;因此,我必须检查我的数据库结构和命名convention@cnizzardini所以没有办法个性化表之间的join on字段?