Cake PhP 3 Saving related data in multiple levels

I have a data model where I register user-> client-> contactinfo-> email / phone

Each one is a different table with their respective controllers and all related.

When I try to save the user, I use the following code:

 $users = TableRegistry::get('Users');
        $user = $users->newEntity($data, [
            'associated' => ['Customers'],['Contactinfos'],['Phones'],['Emails']
        ]);
        $users->save($user);

      

But this only saves the user and the client.

This is the request data:

Array
(
[customers] => Array
    (
        [Customer] => Array
            (
                [name] => stackovertest
                [last_name] => stackovertest
                [ssn] => stackovertest
                [address] => stackovertest
                [gender] => male
                [birth] => Array
                    (
                        [year] => 2015
                        [month] => 05
                        [day] => 20
                    )

            )

    )

[emails] => Array
    (
        [Email] => Array
            (
                [email] => stackovertest@test.com
            )

    )

[password] => stackovertest
[phones] => Array
    (
        [Phone] => Array
            (
                [number] => stackovertest
            )

    )

[role_id] => 0
[activation_code] => a026698222d0479252c2712d3d696fbcca8a766d
[tokentime] => 1432141608
[status] => 
[email] => stackovertest@test.com
[contactinfos] => Array
    (
        [contactinfo] => Array
            (
            )

    )

)

      

Basically the contactinfo table will have an id (auto-increment in the database) and a customer id. And every registered email and phone will have a contact information id.

This is the data of the $ user array:

App\Model\Entity\User Object
 (
[new] => 1
[accessible] => Array
    (
        [email] => 1
        [password] => 1
        [status] => 1
        [activation_code] => 1
        [tokentime] => 1
        [role_id] => 1
        [role] => 1
        [customers] => 1
    )

[properties] => Array
    (
        [customers] => Array
            (
                [0] => App\Model\Entity\Customer Object
                    (
                        [new] => 1
                        [accessible] => Array
                            (
                                [name] => 1
                                [last_name] => 1
                                [ssn] => 1
                                [gender] => 1
                                [birth] => 1
                                [address] => 1
                                [user_id] => 1
                                [user] => 1
                                [aoffers] => 1
                                [hoffers] => 1
                                [poffers] => 1
                            )

                        [properties] => Array
                            (
                                [name] => stackovertest
                                [last_name] => stackovertest
                                [ssn] => stackovertest
                                [address] => stackovertest
                                [gender] => male
                                [birth] => Cake\I18n\Time Object
                                    (
                                        [time] => 2015-05-20T00:00:00+0000
                                        [timezone] => UTC
                                        [fixedNowTime] => 
                                    )

                            )

                        [dirty] => Array
                            (
                                [name] => 1
                                [last_name] => 1
                                [ssn] => 1
                                [address] => 1
                                [gender] => 1
                                [birth] => 1
                            )

                        [original] => Array
                            (
                            )

                        [virtual] => Array
                            (
                            )

                        [errors] => Array
                            (
                            )

                        [repository] => Customers
                    )

            )

        [password] => stackovertest
        [role_id] => 0
        [activation_code] => a026698222d0479252c2712d3d696fbcca8a766d
        [tokentime] => 1432141608
        [status] => 
        [email] => stackovertest@test.com
    )

[dirty] => Array
    (
        [customers] => 1
        [password] => 1
        [role_id] => 1
        [activation_code] => 1
        [tokentime] => 1
        [status] => 1
        [email] => 1
    )

[original] => Array
    (
    )

[virtual] => Array
    (
    )

[errors] => Array
    (
    )

[repository] => Users
)

      

You can see that it doesn't include the contactinfo object, so it won't save it.

How can I fix this? Or is there another way to store multiple levels of related data?

+2


source to share


1 answer


Hey, I managed to fix this problem.

First, the save function should look like this:

        $user = $users->newEntity($data, [
            'associated' => ['Customers','Customers.Contactinfos','Customers.Contactinfos.Phones','Customers.Contactinfos.Emails']
        ]);
        $users->save($user);

      

Second, the request must respect all levels like this:



Array
(
[customers] => Array
    (
        [Customer] => Array
            (
                [name] => test2
                [last_name] => test2
                [contactinfos] => Array
                    (
                        [Contactinfo] => Array
                            (
                                [emails] => Array
                                    (
                                        [Email] => Array
                                            (
                                                [address] => test2@test.test
                                            )

                                    )

                                [phones] => Array
                                    (
                                        [Phone] => Array
                                            (
                                                [number] => test2
                                            )

                                    )

                            )

                    )

                [ssn] => test2
                [address] => test2
                [gender] => male
                [birth] => Array
                    (
                        [year] => 2015
                        [month] => 05
                        [day] => 20
                    )

            )

    )

[password] => test2
[role_id] => 0
[activation_code] => 91c40a9979fcc564a5f27ff09317d6e2f4fdcd58
[tokentime] => 1432154711
[status] => 
[email] => test2@test.test
)

      

And my ctp looks like this:

 <fieldset>
    <legend><?= __('Add User') ?></legend>
    <?php
    echo $this->Form->input('customers.Customer.name');
    echo $this->Form->input('customers.Customer.last_name');
    echo $this->Form->input('customers.Customer.contactinfos.Contactinfo.emails.Email.address', array('label' => 'Email'));
    echo $this->Form->input('password');
    echo $this->Form->input('customers.Customer.contactinfos.Contactinfo.phones.Phone.number',  array('label' => 'Phone'));
    echo $this->Form->input('customers.Customer.ssn', array('label' => 'Cedula'));
    echo $this->Form->input('customers.Customer.address');
    echo $this->Form->input('customers.Customer.gender', array(
        'options' => array('male' => 'Male','female' => 'Female')
    ));
    echo $this->Form->input('customers.Customer.birth');
    echo $this->Form->input('role_id', ['options' => $roles]);
    ?>
</fieldset>

      

+2


source







All Articles