2

I am trying to debug something related to an issue I am facing. The bottom line is that entity load is not returning the peroper values for some reason. For example:

$cids = array(119570363);
$comments = comment_load_multiple($cids);
dpm($comments); 

return an empty array although the comment with id=119570363 exists in the database. I traced entity load function the the comment controller which inherits the load method from DrupalDefaultEntityController.While looking at the method I failed to notice anything that would cause this. Perhaps I am missing something. Are there implicit conditions on the load? Also, I tried with both $reset = FALSE and $reset = TRUE .

4
  • The same code works for me - there is an issue elsewhere. I notice the comment id is quite high - do you really have that many comments on your site ? Jul 25, 2013 at 11:16
  • yes 90 000 000 + comments
    – awm
    Jul 25, 2013 at 14:51
  • 2
    comment_load will only load a comment if the node and the user linked to the comment both exist. Are you sure they do ? What happens if you run this query on your database: SELECT cid FROM comment INNER JOIN node ON node.nid = comment.nid INNER JOIN users ON users.uid = comment.uid WHERE cid = 119570363; Jul 25, 2013 at 15:48
  • Thank You, this answer my question Are there implicit conditions on the load?. I actually wrote a script that get the ids and check those that don't load using entity load. Some are missing users and others are yet to be discovered. Step debuggin all the way. Thanks Again, submit the answer and I shall accept your answer. Can you please tell me how you did find out about the user? because I traced all the methods and did not see anything about it nor anything in the documentation.
    – awm
    Jul 25, 2013 at 16:09

1 Answer 1

1

comment_load_multiple will only load a comment if the node and the user linked to the comment both exist.

You can see this in the code for CommentController::buildQuery where it does an INNER JOIN on both the node and user tables. Note that if you trace through the execution of comment_load_multiple you have to pass in the RESET argument (or clear the caches before hand) otherwise you will get the cached result, and will never make it into CommentController::buildQuery.

Now the reason CommentController::buildQuery does an INNER JOIN rather than a LEFT JOIN (and the reason I didn't add this as an answer straight away) is that you are not supposed to be in a situation where you have a comment that does not have a node or a user.

You can see this because:

So the problem you are seeing suggests your database is somewhat inconsistent. This could happen if you deleted nodes or users without using the proper API, though it could happen for other reasons (eg. sometimes when dumping and re-saving MySql database you might loose the entry for the anonymous user - it has id 0, and must be in the database users table).

Another note: If this is the only issue affecting the loading then you can get the comments that won't load by doing an SQL query:

SELECT cid
FROM comment
LEFT JOIN node ON node.nid = comment.nid
LEFT JOIN users ON users.uid = comment.uid
WHERE (node.nid IS NULL)
      OR (users.uid IS NULL)
1
  • Thank you. It is migrated database which explains why this happens.
    – awm
    Jul 26, 2013 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.