Hii there,
I’m trying to get entries from the Comments
table in my app.
Unfortunately, this doesn’t really work out right now as there are some “rules” to loading them than I can not get working by myself.
A the entities have to relevant objects: reply
and status
.
a reply
is an association to another comment (to which the current comment is a reply) and the status
is well… the status of the comment (eg. spam, private or public).
Now what I’m trying to do is selecting a comment
by id:
$comment = $this->Comments->findById(15);
This is pretty easy, but here comes the tricky part: I want to contain
the Reply
but, I want to contain it only if the status is in the conditions
array.
the conditions array is built like this:
$conditions [['status' => 1]];
if($this->user) {
$conditions[]['user_id'] = $this->user->id;
}
// ... more conditions here
So this code should only contain a reply if the reply’s status
is 1
or if the user is logged in, the user_id
is the id of the logged in user.
I wonder how I should write this…
I have already come up with this piece of code, but this throw me an error:
Integrity constraint violation: 1052 Column ‘status’ in on clause is ambiguous
$comment = $this->Comments
->findById($this->request->getParam('id'))
->contain('Reply', function(Query $q) {
$conditions = [['status' => 1]];
// Check if the user is logged in
if($this->user) {
$conditions[]['user_id'] = $this->user->id;
// Check if the user may view pending comments
if($this->user->hasRight('view_pending_comments')) {
$conditions[]['status'] = 0;
}
// Check if the user may view spam comments
if($this->user->hasRight('view_spam_comments')) {
$conditions[]['status'] = 2;
}
}
return $q->where(['OR' => $conditions]);
})
->first();
It results in the following SQL query:
SELECT Comments.id AS `Comments__id`, Comments.user_id AS `Comments__user_id`, Comments.reply AS `Comments__reply`, Comments.body AS `Comments__body`, Comments.posted AS `Comments__posted`, Comments.modified AS `Comments__modified`, Comments.status AS `Comments__status`, Reply.id AS `Reply__id`, Reply.user_id AS `Reply__user_id`, Reply.reply AS `Reply__reply`, Reply.body AS `Reply__body`, Reply.posted AS `Reply__posted`, Reply.modified AS `Reply__modified`, Reply.status AS `Reply__status` FROM comments Comments LEFT JOIN comments Reply ON (status = :c0 AND Reply.id = (Comments.reply)) WHERE Comments.id = :c1 LIMIT 1