# Cakephp3 find result in cakephp2 array style

**URL:** <https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271>\
**Category:** Need Help\
**Created:** [September 26, 2017, 7:23pm UTC](https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271 "2017-09-26T19:23:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xv1t](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xv1t/32/821_2.png) [@xv1t](https://discourse.cakephp.org/u/xv1t)\
**Post date:** [September 26, 2017, 7:23pm UTC](https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271/1 "2017-09-26T19:23:52Z")

</div>

I migrated to CakePHP3

How make result array in the CakePHP2 style, for example:  
tables list:

1. articles
2. article\_types
3. producers

articles fields:

1. id
2. name
3. article\_type\_id
4. producer\_id  
in CakePHP2, find with assoc

```php
$articles = $this->Article->find('all');
//result
$articles = [
  [
    'Article' => [
      'id' => 1,
      'name' => 'Monte',
      'article_type_id' => 1,
      'producer_id' => 3
    ],
    'ArticleType' => [
       'id' => 1,
       'name' => 'Books'
    ],
    'Producer' => [
      'id' => 3,
      'name' => 'Printhouse'
    ]
  ]
];

```

in cakephp3

```php
$articles = [
      'id' => 1,
      'name' => 'Monte',
      'article_type_id' => 1,
      'producer_id' => 3,
      'article_type' => [
         'id' => 1,
         'name' => 'Books'
      ],
      'producer' => [
         'id' => 3,
         'name' => 'Printhouse'
      ]
];

```

I want put fields of main model

```php
      'id' => 1,
      'name' => 'Monte',
      'article_type_id' => 1,
      'producer_id' => 3,

```

to a array:

```php
[
  'article' => [
      'id' => 1,
      'name' => 'Monte',
      'article_type_id' => 1,
      'producer_id' => 3,
  ],
  /*other associate models*/
]

```

Fields in array of main model are not under modelname. Why?  
For a JSON logic i need to structured data in each datum

```php
[
    'model1' => [/*fields*/],
    'model2' => [/*fields*/],
    'model3' => [/*fields*/],
]

```

How can this? manually recreate array or in the cakephp3 have a `magic` option for this results?

---

<div class="post-metadata">

**Author:** ![rrd](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/rrd/32/113_2.png) [@rrd](https://discourse.cakephp.org/u/rrd)\
**Post date:** [September 26, 2017, 7:37pm UTC](https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271/2 "2017-09-26T19:37:52Z")

</div>

I would change the json logic also.

If you do not want to do that use mapreduce  
[https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#map-reduce](https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#map-reduce)

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [September 27, 2017, 4:39pm UTC](https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271/3 "2017-09-27T16:39:38Z")

</div>

$articles = $this-\>Articles-\>find(‘all’)-\>toArray();

---

<div class="post-metadata">

**Author:** ![xv1t](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xv1t/32/821_2.png) [@xv1t](https://discourse.cakephp.org/u/xv1t)\
**Post date:** [September 27, 2017, 4:49pm UTC](https://discourse.cakephp.org/t/cakephp3-find-result-in-cakephp2-array-style/3271/4 "2017-09-27T16:49:08Z")

</div>

It not for me. The same thing. Fields of main Table in the first deep of the result `datum`, but not in the `'Model'` key

```php
[
	(int) 0 => object(App\Model\Entity\Article) {

		'id' => (int) 1,
		'name' => 'tezt1',
		'description' => '',
		'article_good_id' => (int) 3,
		'disabled' => false,
		'date_expired' => null,
		'article_type_id' => (int) 1,
		'producer_company_id' => (int) 2,
		'supplier_company_id' => (int) 9,
		'created' => null,
		'updated' => object(Cake\I18n\FrozenTime) {

			'time' => '2017-09-18T23:20:57+00:00',
			'timezone' => 'UTC',
			'fixedNowTime' => false
		
		},
		'created_user_id' => null,
		'updated_user_id' => null,
		'supplier_company' => object(App\Model\Entity\Company) {

			'id' => (int) 9,
			'name' => 'DANOBAT',
			'comment' => '!!!!!!!',
			'disabled' => false,
			'created' => null,
			'updated' => object(Cake\I18n\FrozenTime) {

				'time' => '2017-09-18T17:40:55+00:00',
				'timezone' => 'UTC',
				'fixedNowTime' => false
			
			},
			'created_user_id' => null,
			'updated_user_id' => null,
			'[new]' => false,
			'[accessible]' => [
				'name' => true,
				'comment' => true,
				'disabled' => true,
				'created' => true,
				'updated' => true,
				'created_user_id' => true,
				'updated_user_id' => true,
				'created_user' => true,
				'updated_user' => true,
				'company_group_data' => true
			],
			'[dirty]' => [],
			'[original]' => [],
			'[virtual]' => [],
			'[errors]' => [],
			'[invalid]' => [],
			'[repository]' => 'SupplierCompanies'
		
		},
		'producer_company' => object(App\Model\Entity\Company) {

			'id' => (int) 2,
			'name' => 'ADIRA',
			'comment' => '',
			'disabled' => false,
			'created' => null,
			'updated' => object(Cake\I18n\FrozenTime) {

				'time' => '2017-09-19T00:36:15+00:00',
				'timezone' => 'UTC',
				'fixedNowTime' => false
			
			},
			'created_user_id' => null,
			'updated_user_id' => null,
			'[new]' => false,
			'[accessible]' => [
				'name' => true,
				'comment' => true,
				'disabled' => true,
				'created' => true,
				'updated' => true,
				'created_user_id' => true,
				'updated_user_id' => true,
				'created_user' => true,
				'updated_user' => true,
				'company_group_data' => true
			],
			'[dirty]' => [],
			'[original]' => [],
			'[virtual]' => [],
			'[errors]' => [],
			'[invalid]' => [],
			'[repository]' => 'ProducerCompanies'
		
		},
		'[new]' => false,
		'[accessible]' => [
			'name' => true,
			'description' => true,
			'article_good_id' => true,
			'disabled' => true,
			'date_expired' => true,
			'article_type_id' => true,
			'producer_company_id' => true,
			'supplier_company_id' => true,
			'created' => true,
			'updated' => true,
			'created_user_id' => true,
			'updated_user_id' => true,
			'article_good' => true,
			'article_type' => true,
			'producer_company' => true,
			'supplier_company' => true,
			'created_user' => true,
			'updated_user' => true,
			'good_data' => true,
			'goods' => true
		],
		'[dirty]' => [],
		'[original]' => [],
		'[virtual]' => [],
		'[errors]' => [],
		'[invalid]' => [],
		'[repository]' => 'Articles'
	
	},
    /*and others...*/
]

```
