php - Cake PhP 3 多级保存关联数据

标签 php cakephp cakephp-3.0

我有一个数据模型,我在其中注册用户->客户->联系信息->电子邮件/电话

其中每一个都是不同的表,具有各自的 Controller 和关联的所有内容。

当我尝试保存用户时,我使用以下代码:

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

但它只拯救了用户和客户。

这是请求数据:

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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c3f382d2f27233a293e38293f380c38293f38622f2321" rel="noreferrer noopener nofollow">[email protected]</a>
            )

    )

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

    )

[role_id] => 0
[activation_code] => a026698222d0479252c2712d3d696fbcca8a766d
[tokentime] => 1432141608
[status] => 
[email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47343326242c283122353322343307332234336924282a" rel="noreferrer noopener nofollow">[email protected]</a>
[contactinfos] => Array
    (
        [contactinfo] => Array
            (
            )

    )

)

基本上,contactinfo 表将有一个 id(在数据库中自动递增)和客户的 id。每个注册的电子邮件和电话都会有联系信息的 ID。

这是 $user 数组数据:

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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e6e1f4f6fefae3f0e7e1f0e6e1d5e1f0e6e1bbf6faf8" rel="noreferrer noopener nofollow">[email protected]</a>
    )

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

您可以看到它不包含 contactinfo 对象,因此不会保存它。

我该如何解决这个问题?或者是否有不同的方式来保存多个级别的关联数据?

最佳答案

嘿,我设法解决了这个问题。

首先,保存函数应该像这样:

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

其次,请求应该尊重所有级别,如下所示:

Array
(
[customers] => Array
    (
        [Customer] => Array
            (
                [name] => test2
                [last_name] => test2
                [contactinfos] => Array
                    (
                        [Contactinfo] => Array
                            (
                                [emails] => Array
                                    (
                                        [Email] => Array
                                            (
                                                [address] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c38293f387e0c38293f386238293f38" rel="noreferrer noopener nofollow">[email protected]</a>
                                            )

                                    )

                                [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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acd8c9dfd89eecd8c9dfd882d8c9dfd8" rel="noreferrer noopener nofollow">[email protected]</a>
)

我的 ctp 看起来像这样:

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

关于php - Cake PhP 3 多级保存关联数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30356200/

相关文章:

mysql - CakePHP 3 发布方法 '403 Forbidden'

php - CakePHP 3 中的 MVC - 模型和 View 之间的关注点分离

php - 尝试将 Joomla 网站移至新帐户时出现 SQL 错误

php - 在 PHP 中为控制台输出自动换行多字节字符

php - Angularjs如何获取服务中$http响应返回的Json格式的解析数据

cakephp - 在 CakePHP 中包含登录/注销链接的最佳方式是什么?

php - 如何阻止固定位置 div 重叠?

电子邮件上的 Cakephp 2.0 SMTP 设置不起作用

php - 无法定位与分页兼容的对象。使用 CakePHP

cakephp - 如何在 CakePHP v3 中动态删除关联