首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP 2.0 containable返回意外结果

CakePHP 2.0 containable返回意外结果
EN

Stack Overflow用户
提问于 2019-01-14 04:34:07
回答 1查看 28关注 0票数 0

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

代码语言:javascript
复制
class OrganisationAccount
    belongsTo Account
    belongsTo Organisation

class Account
    hasOne User

class Organisation
    belongsTo Account

class User
    belongsTo Account

然后,我使用以下设置使用CakePHP find执行find all -

代码语言:javascript
复制
$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'
            ]
        ]
    ]
);

现在,如果我使用上面的代码执行查找,我会得到以下结果-

代码语言:javascript
复制
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字段之上,

代码语言:javascript
复制
$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'
            ]
        ]
    ]
);

我得到了以下结果-

代码语言:javascript
复制
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的错误,还是我做错了什么?任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-01-22 19:55:47

如果你在OrganizationsAccounts之间有多对多的关系OrganizationAccount,那么manyToMany就会被Organization belongsTo Account搞乱。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54172968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档