CakePHP 2.0可包含返回意外结果

时间:2019-01-13 20:34:07

标签: php find cakephp-2.0 containable

我有以下课程和相关的数据库结构

class OrganisationAccount
    belongsTo Account
    belongsTo Organisation

class Account
    hasOne User

class Organisation
    belongsTo Account

class User
    belongsTo Account

然后我使用CakePHP find通过以下设置执行所有查找-

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

                )

        )

)

我期望的是什么。

但是如果我将“帐户”字段放在查找结果的字段数组中的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
                )

        )

)

您可以看到,即使字段数组包含相同的配置,记录中的Account索引中的Account索引也不同于先前的结果。

查找数组是自动生成的。

这是CakePHP的错误,还是我做错了什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果您在OrganizationAccountOrganizations之间有多对多的关系Accounts,那么Organization belongsTo Account会弄乱manyToMany