$query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user');
  $query->entityCondition('bundle', 'user');
  $query->entityCondition('entity_id', 0, '>');
  $query->count();
  $total_users = $query->execute();

Results in:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bundle' in 'having clause': SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {users} users WHERE (uid > :db_condition_placeholder_0) HAVING (bundle = :db_condition_placeholder_1) ) subquery; Array ( [:db_condition_placeholder_0] => 0 [:db_condition_placeholder_1] => user ) in EntityFieldQuery->execute() (line 1012 of /home/dave/Dropbox/Projects/drupal7dev/includes/entity.inc).

Comments

bojanz’s picture

Assigned: Unassigned » bojanz

Let me take a look.

bfroehle’s picture

Well, the problem is the selectQuery

SELECT users.uid AS entity_id, :entity_type AS entity_type, NULL AS revision_id, :bundle AS bundle
FROM 
{users} users
WHERE  (uid > :db_condition_placeholder_0) 
HAVING  (bundle = :db_condition_placeholder_1)

gets turned into countQuery incorrectly:

SELECT COUNT(*) AS expression
FROM 
(SELECT 1 AS expression
FROM 
{users} users
WHERE  (uid > :db_condition_placeholder_0) 
HAVING  (bundle = :db_condition_placeholder_1) ) subquery
damien tournoud’s picture

No, the problem is that the user entity has no bundle.

damien tournoud’s picture

Title: EntityFieldQuery with user entity type and bundle and set to count fails » EntityFieldQuery fails for entities that have no bundle
bfroehle’s picture

Title: EntityFieldQuery fails for entities that have no bundle » EntityFieldQuery with user entity type and bundle and set to count fails
Status: Active » Needs review
StatusFileSize
new754 bytes

Inspired by #423888-16: Use subqueries for ->countQuery(), at least for MySQL, here is a band-aid which demonstrates that the sample code in the original post isn't crazy.

The real issue here is SelectQuery::countQuery() inappropriately removes variables defined in expressions which also appear in WHERE or HAVING clauses.

chx’s picture

Title: EntityFieldQuery with user entity type and bundle and set to count fails » SelectQuery::countQuery() inappropriately removes variables
Version: 7.x-dev » 8.x-dev
chx’s picture

Component: base system » database system
sun’s picture

Status: Needs review » Needs work
Issue tags: +Needs backport to D7
+++ includes/entity.inc
@@ -1177,7 +1177,11 @@ class EntityFieldQuery {
+      $count_query = db_select($select_query);

Wouldn't a simple clone be faster/more appropriate?

Powered by Dreditor.

bfroehle’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB

Wouldn't a simple clone be faster/more appropriate?

I have no idea, but the patch is attached.

The whole thing is just a giant band-aid to a larger problem, alluded to in comment #5.

Status: Needs review » Needs work
dave reid’s picture

Title: SelectQuery::countQuery() inappropriately removes variables » EntityFieldQuery fails for entities that have no bundle
Component: database system » entity system
Assigned: bojanz » Unassigned
Priority: Major » Normal
Issue tags: +EntityFieldQuery

Holy crap this issue got derailed. The original problem here is that the SQL should not result in a bundle condition if $entity_info[$entity_type]['entity keys']['bundle'] does not exist. That's *IT*.

chx is filing the DBTNG issue.

dave reid’s picture

Assigned: Unassigned » dave reid
Status: Needs work » Needs review
StatusFileSize
new1.7 KB

Here's a patch that at least fixes the bug by using the same logic as the revision key

chx’s picture

bfroehle’s picture

I can confirm that the patch in #12 fixes the bug in the original post. Patch looks very sane to me. Sorry for unintentionally hijacking the issue.

dave reid’s picture

More complete patch with a version with tests modified and tests unmodified to test that this should be expected to fail.

We had a problem with our entity field tests - we created a new controller class for an entity type that did not have a bundle column - which is wrong. DrupalDefaultEntityController is perfectly capable of working with entities that have no bundle, just like user and file entities already.

dave reid’s picture

This time without the unrelated changes.

dave reid’s picture

Another LOL discovered by this patch - all our test entities were using 'name' => t('Name of entity') and not actually using the correct key of 'label' which causes a crap-ton of PHP notices locally but doesn't seem to come through from the testing bot.

dave reid’s picture

Status: Needs review » Needs work
Issue tags: +Needs backport to D7, +EntityFieldQuery

The last submitted patch, 1054168-efq-entities-no-bundle-test-only-expected-fail.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review

Officially needs review now.

dave reid’s picture

bojanz’s picture

StatusFileSize
new6.55 KB

#16 still applies to D7 and makes sense.
+1 for RTBC.

Attaching a reroll for D8.

bojanz’s picture

StatusFileSize
new6.55 KB

Maybe the bot is ignoring the patch because of the "-d8" prefix in the filename? Let's try again.

bojanz’s picture

StatusFileSize
new6.53 KB

Updated comment.

chx’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

damien tournoud’s picture

Status: Reviewed & tested by the community » Needs work

field_sql_storage_field_storage_query() can also filter by bundle, and currently builds the entity/property part of the query itself. We need to refactor it so that it uses EntityFieldQuery::propertyQuery() instead.

Tests also need to be extended to cover queries that have entity_type+bundle+field conditions.

dave reid’s picture

Status: Needs work » Reviewed & tested by the community

Sounds like a good follow-up to me. Can we just get the bug fix in?

damien tournoud’s picture

Status: Reviewed & tested by the community » Needs work

@Dave Reid, you misunderstood: the same bug issue around bundles likely applies when there is a field condition:

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user');
  $query->entityCondition('bundle', 'user');
  $query->fieldCondition('field_toto', 'value', 1);
ParisLiakos’s picture

here is an issue, i believe is the same problem
#1511132: Database error when creating a new list

chx’s picture

Version: 8.x-dev » 7.x-dev
Issue tags: -Needs backport to D7

EntityFieldQuery has been removed from D8.

damienmckenna’s picture

Assigned: dave reid » Unassigned

Unassigning it to imply anyone can pick it up.

The last submitted patch, 16: 1054168-efq-entities-no-bundle.patch, failed testing.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.