cakephp 3.x 将数据保存到具有很多关系的 2 个表中时数据未保存
cakephp 3.x data not saving when Saving data into 2 tables with has many relation
cakephp 3.x 将数据保存到 2 tables 时数据未保存 有很多关系
我有两篇 table 的文章和评论,评论有字段 article_id 它们之间的关系我们的文章有许多评论和评论属于文章,我的 article/add 中有以下代码.ctp 查看文件
<?php
$this->Form->create($article)
echo $this->Form->input('title');
echo $this->Form->input('status');
echo $this->Form->input('comments.0.title');
echo $this->Form->input('comments.0.description');
$this->Form->end()
?>
这是我的控制器代码
public function add() {
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Commments']
]);
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
}
$this->set(compact('article'));
$this->set('_serialize', ['article']);
}
这是我的文章模型
<?php
class ArticlesTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('articles');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Comments', [
'foreignKey' => 'article_id'
]);
}
}
?>
这是我的评论模型
<?php
class CommentsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('comments');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Articles', [
'foreignKey' => 'article_id',
'joinType' => 'INNER'
]);
}
}
?>
当我在补丁实体之后打印 $article 而不是显示下面的数组时,它不会创建评论模型对象,它会在文章模型对象下创建简单数组
App\Model\Entity\Article Object
(
[title] => Article1
[status] => 1
[comments] => Array
(
[0] => Array
(
[title] => Article 1 Comment 1
[description] => mnftrduio
)
)
[[new]] => 1
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
[title] => 1
[status] => 1
[comments] => 1
)
但是数据只保存在文章table中而不是评论table
您的代码没有问题,只是缺少一些回显、分号和三重 'm'
的错误输入注释
$this->Form->create($article) //missing echo and semicolon;
echo $this->Form->create($article); //the correct one
$this->Form->end() //missing submit button
echo $this->Form->button(__('Submit')); //the correct one
echo $this->Form->end(); //the correct one
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Commments']
]); //mistype comments with triple "m"
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Comments']
]);// the correct one
更正后,它应该可以工作了:)
cakephp 3.x 将数据保存到 2 tables 时数据未保存 有很多关系
我有两篇 table 的文章和评论,评论有字段 article_id 它们之间的关系我们的文章有许多评论和评论属于文章,我的 article/add 中有以下代码.ctp 查看文件
<?php
$this->Form->create($article)
echo $this->Form->input('title');
echo $this->Form->input('status');
echo $this->Form->input('comments.0.title');
echo $this->Form->input('comments.0.description');
$this->Form->end()
?>
这是我的控制器代码
public function add() {
$article = $this->Articles->newEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Commments']
]);
if ($this->Articles->save($article)) {
$this->Flash->success(__('The article has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The article could not be saved. Please, try again.'));
}
}
$this->set(compact('article'));
$this->set('_serialize', ['article']);
}
这是我的文章模型
<?php
class ArticlesTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('articles');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Comments', [
'foreignKey' => 'article_id'
]);
}
}
?>
这是我的评论模型
<?php
class CommentsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('comments');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Articles', [
'foreignKey' => 'article_id',
'joinType' => 'INNER'
]);
}
}
?>
当我在补丁实体之后打印 $article 而不是显示下面的数组时,它不会创建评论模型对象,它会在文章模型对象下创建简单数组
App\Model\Entity\Article Object
(
[title] => Article1
[status] => 1
[comments] => Array
(
[0] => Array
(
[title] => Article 1 Comment 1
[description] => mnftrduio
)
)
[[new]] => 1
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
[title] => 1
[status] => 1
[comments] => 1
)
但是数据只保存在文章table中而不是评论table
您的代码没有问题,只是缺少一些回显、分号和三重 'm'
的错误输入注释$this->Form->create($article) //missing echo and semicolon;
echo $this->Form->create($article); //the correct one
$this->Form->end() //missing submit button
echo $this->Form->button(__('Submit')); //the correct one
echo $this->Form->end(); //the correct one
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Commments']
]); //mistype comments with triple "m"
$article = $this->Articles->patchEntity($article, $this->request->data, [
'associated' => ['Comments']
]);// the correct one
更正后,它应该可以工作了:)