【问题标题】:CakePHP 2.0 containable returning unexpected resultCakePHP 2.0 可包含返回意外结果
【发布时间】:2019-01-13 20:34:07
【问题描述】:

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

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
                        )

                )

        )

)

这是我所期待的。

但如果我将 Account 字段放在查找的 fields 数组中的 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 的错误还是我做错了什么?任何帮助将不胜感激。

【问题讨论】:

    标签: php find cakephp-2.0 containable


    【解决方案1】:

    如果您在OrganizationsAccounts 之间存在多对多关系OrganizationAccount,那么Organization belongsTo Account 会破坏manyToMany

    【讨论】:

      猜你喜欢
      • 2016-09-22
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多