Cake PhP 3以多个级别保存相关数据 [英] Cake PhP 3 Saving associated data in multiples levels
问题描述
我有一个数据模型,其中我注册了一个user-> customer-> contactinfo-> email / phone
每一个都是不同的表,
当我尝试保存用户时,我使用以下代码:
$ users = TableRegistry :: get('Users');
$ user = $ users-> newEntity($ data,[
'associated'=> ['Customers'],['Contactinfos'],['Phones'], ]
]);
$ users-> save($ user);
但它只保存用户和客户。
这是请求数据:
Array
(
[customers] =& array
(
[Customer] => Array
(
[name] => stackovertest
[last_name] => stackovertest
[ssn ] => stackovertest
[address] => stackovertest
[gender] => male
[birth] =& => 2015
[month] => 05
[day] => 20
)
)
) b
$ b [email] => Array
(
[Email] => Array
(
[email] => stackovertest@test.com
)
)
[password] => stackovertest
[phones] => array
(
[Phone] => Array
(
[number] => stackovertest
)
)$ b b
[role_id] => 0
[activation_code] => a026698222d0479252c2712d3d696fbcca8a766d
[tokentime] => 1432141608
[status] =>
[email] => stackovertest@test.com
[contactinfos] => array
(
[contactinfo] => Array
(
)
)
)
/ code>
基本上contactinfo表将有一个id(在数据库中自动递增)和客户的id。
这是$ user数组数据:
App\Model\Entity\User Object
(
[new] => 1
[accessible] =& b $ b(
[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] =>男
[birth] => Cake\I18n\Time对象
(
[time] => 2015-05-20T00: 00:00 + 0000
[timezone] => UTC
[fixedNowTime] =>
)
)
] =>数组
(
[name] => 1
[last_name] => 1
[ssn] => 1
[address] => 1
[gender] => 1
[birth] => 1
)
[original] =>数组
(
)
[virtual] =>数组
(
)
[errors] => array
(
)
[repository] =>客户
)
)
[password] => stackovertest
[role_id] => 0
[activation_code] => a026698222d0479252c2712d3d696fbcca8a766d
[tokentime] => 1432141608
[status] =>
[email] => stackovertest@test.com
)
[dirty] =>数组
(
[customers] => 1
[password] => 1
[role_id] => 1
[activation_code] => 1
[tokentime] => 1
[status] => 1
[email] => 1
)
[原始] >数组
(
)
[virtual] =>数组
(
)
[errors] => array
(
)
[repository] =>用户
)
您可以看到它不包括contactinfo对象,因此不会保存它。
如何解决这个问题?或者是否有不同的方式来保存多个关联数据级别?
>
首先,保存函数应该如下所示:
$ user = $ users - > newEntity($ data,[
'associated'=> ['Customers','Customers.Contactinfos','Customers.Contactinfos.Phones','Customers.Contactinfos.Emails']
]);
$ users-> save($ user);
其次请求应该尊重所有级别,例如:
数组
(
[customers] => Array
(
[Customer] =& b(
[name] => test2
[last_name] => test2
[contactinfos] => Array
(
[Contactinfo] =& array
(
[email] => Array
(
[Email] => Array
(
[address] => test2 @ test.test
)
)
[phones] => Array
(
[Phone] =>数组
(
[number] => test2
)
)
)
)
[ssn] => test2
[address] => test2
[gender] =>男
[birth] => Array
(
[year] => 2015
[month] => 05
[day] => 20
)
)
)
[password] => test2
[role_id] => 0
[activation_code] => 91c40a9979fcc564a5f27ff09317d6e2f4fdcd58
[tokentime] => 1432154711
[status] =>
[email] => test2@test.test
)
我的ctp看起来像这样:
< fieldset>
< legend><?= __('添加用户')?>< / 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>
I have a data model where I register an user->customer->contactinfo->email/phone
Each one of these are different tables with respective controllers and everything associated.
When I try to save an user I use the following code:
$users = TableRegistry::get('Users');
$user = $users->newEntity($data, [
'associated' => ['Customers'],['Contactinfos'],['Phones'],['Emails']
]);
$users->save($user);
But it only saves the user and the customer.
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 contactinfo table will have an id (autoincremented in the database) and the id of the customer. And each email and phone registered will have the id of the contactinfo.
This is the $user array data:
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 does not include the contactinfo object so it will not save it.
How can I fix this? Or is there a different way to save multiple levels of associated data?
Hey I managed to fix this issue.
First the save function should go like this:
$user = $users->newEntity($data, [
'associated' => ['Customers','Customers.Contactinfos','Customers.Contactinfos.Phones','Customers.Contactinfos.Emails']
]);
$users->save($user);
Second the request should respect all the 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>
这篇关于Cake PhP 3以多个级别保存相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!