我有以下类和相关的数据库结构
class OrganisationAccount
belongsTo Account
belongsTo Organisation
class Account
hasOne User
class Organisation
belongsTo Account
class User
belongsTo Account然后,我使用以下设置使用CakePHP find执行find all -
$OrganisationAccount->Behaviors->load('Containable');
$records = $OrganisationAccount->find(
'all',[
'fields'=>[
$OrganisationAccount->name.'.status',
'Account.email_address',
'Account.last_login',
],
'conditions'=>[
$OrganisationAccount->name.'.organisation_id'=>57
],
'contain'=>[
'Account'=>[
'User'
]
]
]
);现在,如果我使用上面的代码执行查找,我会得到以下结果-
Array
(
[0] => Array
(
[OrganisationAccount] => Array
(
[status] => REGISTERED
)
[Account] => Array
(
[email_address] => pdatar@checkvault.com.au
[last_login] => 2019-01-13 20:13:18
[id] => 44
[User] => Array
(
[id] => 32
[uuid] => 5c3814fc-2868-423f-9242-45b4cbdd56cb
[created] => 2019-01-11 04:01:00
[modified] => 2019-01-11 04:01:00
[account_id] => 44
[first_name] => John
[last_name] => Individual
[gender] => Not specified
[date_of_birth] =>
[display_picture] =>
[mobile] =>
[full_name] => John Individual
)
)
)
)这正是我所期待的。
但如果我将find中的字段数组中的Account字段放在OrganisationAccount字段之上,
$OrganisationAccount->Behaviors->load('Containable');
$records = $OrganisationAccount->find(
'all',[
'fields'=>[
'Account.email_address',
'Account.last_login',
$OrganisationAccount->name.'.status'
],
'conditions'=>[
$OrganisationAccount->name.'.organisation_id'=>57
],
'contain'=>[
'Account'=>[
'User'
]
]
]
);我得到了以下结果-
Array
(
[0] => Array
(
[Account] => Array
(
[email_address] => pdatar@checkvault.com.au
[last_login] => 2019-01-13 20:13:18
[id] => 44
[Account] => Array
(
[email_address] => pdatar@checkvault.com.au
[last_login] => 2019-01-13 20:13:18
[id] => 44
[User] => Array
(
[id] => 32
[uuid] => 5c3814fc-2868-423f-9242-45b4cbdd56cb
[created] => 2019-01-11 04:01:00
[modified] => 2019-01-11 04:01:00
[account_id] => 44
[first_name] => John
[last_name] => Individual
[gender] => Not specified
[date_of_birth] =>
[display_picture] =>
[mobile] =>
[full_name] => John Individual
)
)
)
[OrganisationAccount] => Array
(
[status] => REGISTERED
)
)
)正如您所看到的,该记录在帐户索引内有一个帐户索引,这与之前的结果不同,尽管字段数组包含相同的配置。
find数组是自动生成的。
这是CakePHP的错误,还是我做错了什么?任何帮助都将不胜感激。
发布于 2019-01-22 19:55:47
如果你在Organizations和Accounts之间有多对多的关系OrganizationAccount,那么manyToMany就会被Organization belongsTo Account搞乱。
https://stackoverflow.com/questions/54172968
复制相似问题