distinct() public method

This clause can only be used for select statements. If you wish to filter duplicates based of those rows sharing a particular field or set of fields, you may pass an array of fields to filter on. Beware that this option might not be fully supported in all database systems. ### Examples: Filters products with the same name and city $query->select(['name', 'city'])->from('products')->distinct(); Filters products in the same city $query->distinct(['city']); $query->distinct('city'); Filter products with the same name $query->distinct(['name'], true); $query->distinct('name', true);
public distinct ( array | Cake\Database\ExpressionInterface | string | boolean $on = [], boolean $overwrite = false )
$on array | Cake\Database\ExpressionInterface | string | boolean Enable/disable distinct class or list of fields to be filtered on
$overwrite boolean whether to reset fields with passed list or not
Example #1
0
 /**
  * Tests that it is possible to select distinct rows
  *
  * @return void
  */
 public function testSelectDistinct()
 {
     $query = new Query($this->connection);
     $result = $query->select(['author_id'])->from(['a' => 'articles'])->execute();
     $this->assertCount(3, $result);
     $result = $query->distinct()->execute();
     $this->assertCount(2, $result);
     $result = $query->select(['id'])->distinct(false)->execute();
     $this->assertCount(3, $result);
 }
Example #2
0
 /**
  * Returns the passed query after rewriting the DISTINCT clause, so that drivers
  * that do not support the "ON" part can provide the actual way it should be done
  *
  * @param \Cake\Database\Query $query The query to be transformed
  * @return \Cake\Database\Query
  */
 protected function _transformDistinct($query)
 {
     if (is_array($query->clause('distinct'))) {
         $query->group($query->clause('distinct'), true);
         $query->distinct(false);
     }
     return $query;
 }