Cake\Database\Query::distinct PHP Метод

distinct() публичный Метод

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
    public function distinct($on = [], $overwrite = false)
    {
        if ($on === []) {
            $on = true;
        } elseif (is_string($on)) {
            $on = [$on];
        }
        if (is_array($on)) {
            $merge = [];
            if (is_array($this->_parts['distinct'])) {
                $merge = $this->_parts['distinct'];
            }
            $on = $overwrite ? array_values($on) : array_merge($merge, array_values($on));
        }
        $this->_parts['distinct'] = $on;
        $this->_dirty();
        return $this;
    }

Usage 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);
 }
All Usage Examples Of Cake\Database\Query::distinct