Re-addressing an issue I first mentioned in #564852-22: Subselect with placeholders causes invalid/duplicate placeholders. It seems the SelectQuery::toString() method incorrectly modifies the called object, thus resulting in incorrect query results for more complex queries.

Attached is a (probably still) failing test case for this scenario. The error can also be reproduced with the following code:

 $query = db_select('node', 't')->fields('t', array('uid'))->condition('t.uid', 2);
$query = db_select($query, 't')->fields('t', array('uid'))->condition('t.uid', 1);
(string) $query;
print_r($query->execute()->fetchAll()); 

And regarding Crell's last comment in the other issue:
Select queries are non-reusable, unlike the other builders, for various reasons.
I don't see why this is important here. Calling toString() should hardly count as "using".

Also, can someone tell me how you can find out the real SQL query executed, with all placeholders replaced, etc.? I have again problems with a complex query and suspect it might be a problem with placeholders, but can't test it because of the unhelpful toString() method. Is there any easy way?

CommentFileSizeAuthor
toString_problem_test.patch1.57 KBdrunken monkey

Comments

Status: Needs review » Needs work

The last submitted patch, toString_problem_test.patch, failed testing.

berdir’s picture

Cross-referencing #886970: DB API putting wrong db placeholders in complex queries

While jhodgon found a fix for the that issue, it is quite a message and looks related to this :)

Crell’s picture

Status: Needs work » Closed (duplicate)

See the last comment in the thread linked in #2.

franz’s picture

Status: Closed (duplicate) » Active

I still find this bug when debugging a views query.

If I print $query->__toString() right before it's execution it fails with PDOexception on wrong MySQL syntax.

Crell’s picture

Status: Active » Closed (duplicate)

You're not supposed to be calling __toString() directly, especially on select objects. (Damien has argued that's a design flaw, and he may be right, but it is what it is for Drupal 7.) That's unsupported. If you really need the string for debugging purposes then you need to call preQuery() first.

charlie-s’s picture

Crell, where is preQuery()?