When using an EntityFieldQuery to retrieve comments, it is not possible to filter them by bundle. To reproduce, simply execute the following code:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'comment');
$query->entityCondition('bundle', 'comment_node_article');
$result = $query->execute();

This will throw an exception:


PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'comment.node_type' in 'where clause': SELECT comment.cid AS entity_id, :entity_type AS entity_type, NULL AS revision_id
FROM
{comment} comment
WHERE (comment.node_type = :db_condition_placeholder_0) ; Array
(
[:db_condition_placeholder_0] => comment_node_article
[:entity_type] => comment
)
in EntityFieldQuery->execute() (line 1158 of /var/www/drupal/drupal7/includes/entity.inc).

If something about the above code is just wrong, please correct me, but it seems to work fine with nodes.

Comments

drunken monkey created an issue.

njbarrett’s picture

I am also seeing this bug in core 7.41

njbarrett’s picture

For anyone looking for a fix in the meantime, you can add this hook to your module:

function MY_MODULE_query_comment_bundle_fix_alter(QueryAlterableInterface $query) {
  $query->innerJoin('node', NULL, 'comment.nid = node.nid');
  $conditions = &$query->conditions();
  $conditions[0]['field'] = 'node.type';
  $conditions[0]['value'] = str_replace('comment_node_', '', $conditions[0]['value']);
}

NOTE: If the comment bundle condition is not the first condition you may need to change the '0' index of the $conditions array to match your query config.
Then in your EntityFieldQuery do this:

$query->addTag('comment_bundle_fix');