Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon: how to get distinct models?

Tags:

php

phalcon

Using Phalcon Model, how can I get distinct rows when get rows using the find() method.

like image 444
Handsome Nerd Avatar asked Jun 13 '15 14:06

Handsome Nerd


2 Answers

Using builder:

Basic implementation for later example:

    $queryBuilder = $this->getDI()->getModelsManager()
        ->createBuilder()
        ->addFrom('tableName', 't');

Distinct command:

    $queryBuilder->distinct('t.id');

Column thing works too, but not recommended:

    $queryBuilder->columns('DISTINCT(t.id) AS id')

Using strictly model:

   // we are waiting for it, but may still not be implemented
   TableModel::find(array('distinct' => 'id'))

For count:

   TableModel::count(array("distinct" => "id"));

And less recommended way according to previous answer:

   TableModel::find(array('columns' => 'distinct(id)'))

And link to imo best docs.

Also, there are some issues in Phalcon 2.0.2.

like image 74
yergo Avatar answered Oct 12 '22 03:10

yergo


If you have declared some columns you can use:

 $result = Word::find(['columns'=>'distinct foo']);
like image 4
Handsome Nerd Avatar answered Oct 12 '22 03:10

Handsome Nerd