2

My query looks like:

$test = $this->Producers->find()
    ->hydrate(true)
    ->select(['Producers.id','Producers.name', 'Products.id', 'Products.name']);

    $test->matching(
            'Products.Categories', function ($q) {
                return $q->where(['Categories.id' => 22]);
    } );

This query use inner join and returned data naturally looks like

|Producers_id|Producers_name|Products_id|Products_name|
-------------------------------------------------------
|1           |Canon         |1          |EOS 1000D    |
-------------------------------------------------------
|1           |Canon         |2          |EOS 300D     |
-------------------------------------------------------
|1           |Canon         |3          |EOS 50D      |
-------------------------------------------------------
|3           |Nikon         |6          |D600         |
-------------------------------------------------------
|3           |Nikon         |7          |D100         |
-------------------------------------------------------

In view, after foreach iteration i have producer and product for each row:

=============================
Producer: Canon
-----------------------------
EOS 1000D
=============================
Producer: Canon
-----------------------------
EOS 300D
=============================
Producer: Canon
-----------------------------
EOS 50D
=============================

But i expect formated data like:

=============================
Producer: Canon
-----------------------------
EOS 1000D
-----------------------------
EOS 300D
-----------------------------
EOS 50D
=============================
Producer: Nikon
-----------------------------
D600
-----------------------------
D100
=============================

What is the best data formatting practice? In view, or model/controller? Maybe use mapReduce()?

Maciej Chyra
  • 53
  • 1
  • 8

1 Answers1

0

Best practice or not, using Cake\Collection\Collection, you can group the results:

$collection = new Collection($test);
$byProducer = $collection->groupBy('Producers_name')->toArray();

This would result in:

[Canon] => Array
    (
        [0] => Array
            (
                [Producers_id] => 1
                [Producers_name] => Canon
                [Products_id] => 1
                [Products_name] => EOS 1000D
            )

        [1] => Array
            (
                [Producers_id] => 1
                [Producers_name] => Canon
                [Products_id] => 2
                [Products_name] => EOS 300D
            ) ...
    )

[Nikon] => Array
    (
        [0] => Array
            (
                [Producers_id] => 3
                [Producers_name] => Nikon
                [Products_id] => 6
                [Products_name] => D600
            ) ...
    )
code-kobold
  • 829
  • 14
  • 18
  • 1
    `$test` [**already is a collection**](http://book.cakephp.org/3.0/en/orm/query-builder.html#queries-are-collection-objects) (kind of). Also you'll have to group by `name`, with the query shown by the OP, there will be no `Producers_name` in the resulting entites. – ndm Jun 08 '16 at 21:26
  • code-kobolt: works great, thank You! ndm You have right, only need to: `$byProducer = $test->groupBy('Producers_name')->toArray();` without "`use Cake\Collection\Collection;`" and `$collection = new Collection($test);` – Maciej Chyra Jun 09 '16 at 06:58