0

I have the following class and associated database structure

class OrganisationAccount
    belongsTo Account
    belongsTo Organisation

class Account
    hasOne User

class Organisation
    belongsTo Account

class User
    belongsTo Account

I'm then performing a find all using CakePHP find with the following setup -

$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'
            ]
        ]
    ]
);

Now, if I perform the find using the above I get the following result -

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
                        )

                )

        )

)

Which is what I am expecting.

But if I put the Account fields above the OrganisationAccount fields in the fields array in the find,

$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'
            ]
        ]
    ]
);

I get the following result -

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
                )

        )

)

As you can see the record has an Account index inside an Account index which is different to the previous result even though the field array contained the same configuration.

The find array is autogenerated.

Is this a bug with CakePHP or am I doing something incorrectly? Any help would be much appreciated.

Oerd
  • 2,256
  • 1
  • 21
  • 35
Prathamesh Datar
  • 375
  • 1
  • 4
  • 20

1 Answers1

1

If you have many to many relationship OrganizationAccount between Organizations and Accounts, then Organization belongsTo Account messes up the manyToMany.

Oerd
  • 2,256
  • 1
  • 21
  • 35