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?
| Comment | File | Size | Author |
|---|---|---|---|
| toString_problem_test.patch | 1.57 KB | drunken monkey |
Comments
Comment #2
berdirCross-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 :)
Comment #3
Crell commentedSee the last comment in the thread linked in #2.
Comment #4
franzI 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.
Comment #5
Crell commentedYou'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.
Comment #6
charlie-s commentedCrell, where is preQuery()?