ZF-11392: UNION
Description
Line 295 should be change from:
$this->_parts[self::UNION][] = array($target, $type);
To:
$this->_parts[self::UNION][] = array("({$target})", $type);
Line 295 should be change from:
$this->_parts[self::UNION][] = array($target, $type);
To:
$this->_parts[self::UNION][] = array("({$target})", $type);
Comments
Posted by Christian Ludwig (ludwig) on 2011-05-27T15:20:55.000+0000
There might be some explanation needed.
Without the parentheses you get the wrong results i. e. when you want to order the complete query.
The example in the documentation ([http://framework.zend.com/manual/en/…])
$sql1 = $db->select(); $sql2 = "SELECT ..."; $select = $db->select() ->union(array($sql1, $sql2)) ->order("id");this will output something like:
Here once more the patch:
diff U3B /Zend/Db/Select.php --- Select.php 23775 2011-03-01 17:25:24Z ralph +++ Select.php Working Copy @@ -292,7 +292,7 @@ } foreach ($select as $target) { - $this->_parts[self::UNION][] = array($target, $type); + $this->_parts[self::UNION][] = array('(' . $target . ')', $type); } return $this;Posted by Kim Blomqvist (kblomqvist) on 2011-05-27T17:27:10.000+0000
I can see from mysql docs that parentheses are required, but still tried to write a unit test to see that the current code will fail. However couldn't make it happen. Could you help me with that? I modified {{_selectUniontString()}} method in {{tests/Zend/Db/Select/TestCommon.php}} and got the query like this
and result like this
Looks correct for me.
Posted by Christian Ludwig (ludwig) on 2011-05-27T22:03:16.000+0000
I don't know what your data source is looking like, but the parentheses should always be there. Think about a query where you want to order the results of the second select of the UNION only.
Without parentheses it would end up in your query
Or use a LIMIT for your tests:
against
Posted by Kim Blomqvist (kblomqvist) on 2011-05-28T06:35:46.000+0000
Patch file attached as suggested by the author. Couldn't make a unit test though. Seems like this cannot be reproduced with the current test data.
Christian - thank you for the explanation.
Posted by Kim Blomqvist (kblomqvist) on 2011-05-28T20:47:17.000+0000
Ok, looks like we still need the test. The test below cannot reproduce the issue atm. and should be fixed to do so. I'm pretty sure that I'm missing something and doing it wrong. You can find test data source from {{tests/Db/TestUtil/Common.php}}.
Posted by Kim Blomqvist (kblomqvist) on 2011-05-30T15:05:32.000+0000
See the comment by Christoph Roensch from ZF-4338 ...
bq. I believe this is non-standard SQL, because of that it cannot be included in Zend_Db_Select.
Posted by Pádraic Brady (padraic) on 2011-08-14T12:08:31.000+0000
Marked as Won't Fix on the basis of:
Reporters and Assignee are recommended to fix in ZF2 if still a problem - or open a ZF2 issue in its respect.