[英]cakephp 3 saving associated data
I'm trying to save associated data, but even though it seems to be set right it doesn't commit to the database. 我正在尝试保存关联的数据,但是即使它似乎设置正确,它也不会提交到数据库。 I have to explicitly use the associated table to save the data. 我必须显式使用关联的表来保存数据。 using the 'associated' keyword is ineffective. 使用“关联”关键字无效。
my controller action: 我的控制器动作:
public function test_associated_save()
{
$usersTable = TableRegistry::get('Users');
$test_user = $usersTable->get(21, ['contain' => ['Names']]);
$test_user['names'][0]['middle'] = 'test name';
$usersTable->save($test_user);
$usersTable->save($test_user, ['associated' => ['Names']]);
debug($test_user);
$test_user = $usersTable->get(21, ['contain' => ['Names']]);
debug($test_user);
$test_user['names'][0]['middle'] = 'test name';
$namesTable = TableRegistry::get('Names');
$namesTable->save($test_user['names'][0]);
$test_user = $usersTable->get(21, ['contain' => ['Names']]);
debug($test_user);
}
debug output for the two $test_user first debug 调试两个$ test_user的输出,首先进行调试
object(App\Model\Entity\User) {
'id' => (int) 21,
...
'names' => [
(int) 0 => object(App\Model\Entity\Name) {
'id' => (int) 20,
'user_id' => (int) 21,
'middle' => 'test name',
...
'[accessible]' => [
...
'middle' => true,
...
],
'[dirty]' => [
'middle' => true
],
'[original]' => [
'middle' => ''
],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Names'
},
...
second debug 第二次调试
object(App\Model\Entity\User) {
'id' => (int) 21,
...
'names' => [
(int) 0 => object(App\Model\Entity\Name) {
'id' => (int) 20,
'user_id' => (int) 21,
'middle' => '',
...
'[accessible]' => [
...
'middle' => true,
...
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Names'
},
...
third debug 第三次调试
object(App\Model\Entity\User) {
'id' => (int) 21,
...
'names' => [
(int) 0 => object(App\Model\Entity\Name) {
'id' => (int) 20,
'user_id' => (int) 21,
'middle' => 'test name',
...
'[accessible]' => [
...
'middle' => true,
...
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Names'
},
...
My Users table 我的用户表
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->table('users');
$this->displayField('username');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Names', [
'foreignKey' => 'user_id'
]);
...
}
...
}
My Names table 我的名字表
class NamesTable extends Table { 类NamesTable扩展了Table {
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
$this->table('names');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}
my mysql tables 我的mysql表
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL,
`username` varchar(50) NOT NULL,
...
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `names` (
`id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`middle` varchar(255) NOT NULL,
...
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
don't use array access to change object values. 不要使用数组访问来更改对象值。 you can use patchEntity instead, after creating the changed data array. 创建更改后的数据数组后,可以改用patchEntity 。
$test_data = [
'names' => [
[
'id' => $test_user['names'][0]['id'],
'middle' => 'test name 5'
]
]
];
$usersTable->patchEntity($test_user, $test_data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.