[英]cakephp 3 saving associated data
我正在嘗試保存關聯的數據,但是即使它似乎設置正確,它也不會提交到數據庫。 我必須顯式使用關聯的表來保存數據。 使用“關聯”關鍵字無效。
我的控制器動作:
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);
}
調試兩個$ 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'
},
...
第二次調試
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'
},
...
第三次調試
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'
},
...
我的用戶表
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'
]);
...
}
...
}
我的名字表
類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'
]);
}
我的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;
不要使用數組訪問來更改對象值。 創建更改后的數據數組后,可以改用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.