entity_load_multiple returns empty array although an eitity lives in the database? 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 .
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:


The code for comment_save clearly expects the associated node and user to be set ;
The comment module implements hook_node_delete, and always ensures that comments are deleted when a node is deleted ;
The comment  module implements hook_user_delete, and always ensures that comments are deleted when a user is deleted.


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)