Reputation: 393
I have Comments table where a user can comment on another user like this :
When I use this query :
$comments = TableRegistry::getTableLocator()
->get('Comments')
->find('all');
$query = $comments
->find('all')
->contain(['Users']);
It retrieves the comment but it applies the join only on commented_id . While I want to retrieve The comment object containing its two related users one as commentator , the other as commented ,; So how to build the query ?
This is the CommentsTable :
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;
}
}
Upvotes: 0
Views: 184
Reputation: 63
Chnage your relationships to something like this:
$this->belongsTo('Commenters', [
'className' => 'Users',
'foreignKey' => 'commentator_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'commented_id',
'joinType' => 'INNER'
]);
So then you can contain the two different relationships.
$comments = TableRegistry::getTableLocator()
->get('Comments')
->find('all')
->contain(['Commenters', 'Users']);
And access them like this:
$comment->commenter->name; // Name of the person who commented
$comment->user->name; // Name of the person who was commented on
Upvotes: 1
Reputation: 5662
I'm not sure exactly what output you what but as per my understanding you can use custom join for the same:
$data = $this->Comments->find()
->join([
'Users' => [
'table' => 'users',
'type' => 'INNER',
'conditions' => [
'Users.id=Comments.commentator_id',
'OR' => [
'Users.id=Comments.commented_id'
]
]
]
]);
or you can modify this as per your need.
https://book.cakephp.org/3.0/en/orm/query-builder.html#adding-joins
Hope this will help.
Upvotes: 0