我创建了一个使用CakePHP2的Web应用程序。我使用CakePHP ORM构建了一个查询,但结果并不像我预期的那样。所以我的问题是:是否可以使用CakePHP ORM设置查询以获得与我预期相同的结果。
这是我的查询:
$addresses = $this->User->Profile->Address->AddressType->find('all', array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'addresses',
'alias' => 'Address',
'type' => 'LEFT',
'conditions' => array(
'AddressType.id = Address.address_type_id'
)
),
array(
'table' => 'profiles',
'alias' => 'Profile',
'type' => 'LEFT',
'conditions' => array(
'Address.profile_id = Profile.id'
),
),
array(
'table' => 'users',
'alias' => 'User',
'type' => 'LEFT',
'conditions' => array(
'User.profile_id = Profile.id'
),
)
),
'conditions' => array(
'User.id' => $id
),
//'group' => array('AddressType.id'),
'fields' => array(
'Address.*',
'AddressType.*',
'Profile.id',
'User.id'
),
));
这是我的结果:
array(
(int) 0 => array(
'Address' => array(
'id' => '4',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '5',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '6',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'Test 3',
'zip' => 'Test 3',
'city' => 'Test 3',
'street' => 'Test 3',
'number' => 'Test 3',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 3 => array(
'Address' => array(
'id' => '7',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Test 4',
'zip' => 'Test 4',
'city' => 'Test 4',
'street' => 'Test 4',
'number' => '',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 4 => array(
'Address' => array(
'id' => '8',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test5',
'zip' => 'test5',
'city' => 'test5',
'street' => 'test5',
'number' => 'test5',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 5 => array(
'Address' => array(
'id' => '9',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Tesztecske',
'zip' => 'Test 41',
'city' => 'Test 41',
'street' => 'Test 41',
'number' => 'test',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 6 => array(
'Address' => array(
'id' => '10',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)2222758',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 7 => array(
'Address' => array(
'id' => '13',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'sdfgdfgsdfgdsfg',
'zip' => 'sdfg',
'city' => 'sdfg',
'street' => 'sdfgsdfg',
'number' => 'sdfgsdfg',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
)
)
所以我想要按地址类型名称分组的结果。
array(
'Shipping Address' =>array(
(int) 0 => array(
'Address' => array(
'id' => '4',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '8',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'test5',
'zip' => 'test5',
'city' => 'test5',
'street' => 'test5',
'number' => 'test5',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '6',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '1',
'name' => 'Test 3',
'zip' => 'Test 3',
'city' => 'Test 3',
'street' => 'Test 3',
'number' => 'Test 3',
),
'AddressType' => array(
'id' => '1',
'name' => 'Shipping Address',
'code' => 'shipping_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
),
'test_type' => array(
(int) 0 => array(
'Address' => array(
'id' => '7',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Test 4',
'zip' => 'Test 4',
'city' => 'Test 4',
'street' => 'Test 4',
'number' => '',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '9',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '4',
'name' => 'Tesztecske',
'zip' => 'Test 41',
'city' => 'Test 41',
'street' => 'Test 41',
'number' => 'test',
),
'AddressType' => array(
'id' => '4',
'name' => 'test_type',
'code' => 'test_type'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 2 => array(
'Address' => array(
'id' => '5',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
),
'Invoice Address' =>array(
(int) 0 => array(
'Address' => array(
'id' => '10',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'test - (invoice)2222758',
'zip' => 'test',
'city' => 'test',
'street' => 'test',
'number' => 'test',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
),
(int) 1 => array(
'Address' => array(
'id' => '13',
'profile_id' => '10',
'country_id' => '1',
'address_type_id' => '2',
'name' => 'sdfgdfgsdfgdsfg',
'zip' => 'sdfg',
'city' => 'sdfg',
'street' => 'sdfgsdfg',
'number' => 'sdfgsdfg',
),
'AddressType' => array(
'id' => '2',
'name' => 'Invoice Address',
'code' => 'invoice_address'
),
'Profile' => array(
'id' => '10'
),
'User' => array(
'id' => '10'
)
)
)
)