# fetching result from database in specific format cakephp5

**URL:** <https://discourse.cakephp.org/t/fetching-result-from-database-in-specific-format-cakephp5/11912>\
**Category:** Need Help\
**Tags:** release, help\
**Created:** [February 29, 2024, 11:41am UTC](https://discourse.cakephp.org/t/fetching-result-from-database-in-specific-format-cakephp5/11912 "2024-02-29T11:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtrackadnan](https://avatars.discourse-cdn.com/v4/letter/d/9e8a1a/32.png) [@dtrackadnan](https://discourse.cakephp.org/u/dtrackadnan)\
**Post date:** [February 29, 2024, 11:41am UTC](https://discourse.cakephp.org/t/fetching-result-from-database-in-specific-format-cakephp5/11912/1 "2024-02-29T11:41:27Z")

</div>

I am currently in the process of migrating from CakePHP 2.8 to CakePHP 5.x. In CakePHP 2.8, I used to retrieve results in the following format:

```auto
$this->find('all', array(
    'contain' => array('UserDetail'),
));

Array
(
    [ModelName] => Array
        (
            [id] => 83
            [field1] => value1
            [field2] => value2
            [field3] => value3
        )
        
    [UserDetail] => Array
       (
            [id] => 1
            [field1] => value1
            [field2] => value2
            [field3] => value3
       )
)

```

However, after migrating to CakePHP 5.0, the results are now in the following format:

```auto
    (
    (
        [id] => 83
        [field1] => value1
        [field2] => value2
        [field3] => value3
        [user_detail] => Array
        (
            [id] => 1
            [field1] => value1
            [field2] => value2
            [field3] => value3
        )
    )
)

$query = $this->find('all')->contain(['UserDetail']);
$result = $query->toArray();

```

Is there a way to retrieve the results in CakePHP 5.x in the same format as in CakePHP 2.8?

Thank you for any assistance in this CakePHP 5.x migration from 2.x.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [February 29, 2024, 5:43pm UTC](https://discourse.cakephp.org/t/fetching-result-from-database-in-specific-format-cakephp5/11912/2 "2024-02-29T17:43:18Z")

</div>

Take a look at the [Collection](https://book.cakephp.org/5/en/core-libraries/collections.html) class. I would probably use [Collection::reduce()](https://book.cakephp.org/5/en/core-libraries/collections.html#Cake%5CCollection%5CCollection::reduce); it’s one of my go-to tools.

You can also integrate the data structure transformation into the query (still using Collection tools) using the [Map/Reduce](https://book.cakephp.org/5/en/orm/retrieving-data-and-resultsets.html#modifying-results-with-map-reduce) feature of Queries.

For some reason I find this approach more confusing than writing my structure transformations separately but, every dog to his tree 🙂

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 29, 2024, 6:32pm UTC](https://discourse.cakephp.org/t/fetching-result-from-database-in-specific-format-cakephp5/11912/3 "2024-02-29T18:32:07Z")

</div>

Didn’t like the response you got at [php - fetching result from database in specific format cakephp5 - Stack Overflow](https://stackoverflow.com/questions/78073402/fetching-result-from-database-in-specific-format-cakephp5)?
