Cakephp3 find result in cakephp2 array style


#1

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
$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

$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

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

to a array:

[
  '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

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

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


#2

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


#3

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


#4

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

[
	(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...*/
]