Issues

ZF2-424: Sql query builder doesn't support distinct

Issue Type: Improvement Created: 2012-07-26T13:13:22.000+0000 Last Updated: 2012-10-08T20:15:05.000+0000 Status: Closed Fix version(s): Reporter: Ken Lynch (nyorai) Assignee: Ralph Schindler (ralph) Tags: - Zend\Db

Related issues: Attachments:

Description

The Sql object query builder doesn't support the distinct keyword, which was available in ZF1.

Comments

Posted by Ralph Schindler (ralph) on 2012-07-26T15:59:45.000+0000

It does indeed, you can pass an expression as a column name ($select is an instance of Zend\Db\Sql\Select)

<pre class="highlight">
$select->columns(array(
    'aid' => new Zend\Db\Sql\Expression('DISTINCT artist_id')
));

If you have to ensure that the column is properly quoted for future abstraction purposes:

<pre class="highlight">
$select->columns(array(
    'aid' => new Zend\Db\Sql\Expression(
       'DISTINCT ?',
       'artist_id',
       array(Zend\Db\Sql\Expression::TYPE_IDENTIFIER))
));

Posted by Ken Lynch (nyorai) on 2012-07-27T07:58:15.000+0000

This doesn't feel very obvious and causes an error if the distinct expression doesn't come first (certainly using MySQL). How can I take a base select query and later in another method add a join and make it return distinct rows? In my specific case I have a base query that selects orders, I might modify this with a join against the order items when I want to only select orders that have a certain item. In ZF1 you would simply do the join and call the distinct method.

Posted by Ralph Schindler (ralph) on 2012-07-27T14:24:07.000+0000

You make an excellent point. I'll work on this soon.

Posted by Ralph Schindler (ralph) on 2012-10-08T20:15:05.000+0000

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2509

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts