我有一个表单收集有关文章的数据,我想保存这些数据,以及一个名为Abstract的模型,其中一篇文章有多个摘要。我的模型看起来像这样:
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class AbstractsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Articles');
}
public function validationDefault(Validator $validator)
{
$validator
->notEmpty('body');
return $validator;
}
}
并且
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->hasMany('Abstracts');
}
public function validationDefault(Validator $validator)
{
$validator ->notEmpty('category')
return $validator;
}
}
我的输入表单有一个名为'abstracts.body'的字段,在我的ArticlesController中我有这个功能:
public function add()
{
$data = $this->request->data;
$article = $this->Articles->newEntity($data, [
'associated' => ['Abstracts']
]);
if ($this->request->is('post')) {
$article->user_id = $this->Auth->user('id');
$data['abstracts']['user_id'] = $article->user_id;
$data['abstracts']['approved'] = 0;
$article = $this->Articles->patchEntity($article, $data, [
'associated' => ['Abstracts']
]);
if ($this->Articles->save($article, [ 'validate' => false,
'associated' => ['Abstracts']
]) )
{
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('article', $article);
}
My Abstracts表非常简单:
CREATE TABLE 'abstracts' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'article_id' INTEGER , 'user_id' INTEGER , 'body' TEXT, 'approved' BOOLEAN )
从调试中我可以看到我的$ data(在add()中)有正确的'abstracts'数组,但似乎没有尝试将它保存到数据库中。有人可以指出我的错误吗?谢谢!
答案 0 :(得分:4)
知道了。
我在这里开始出错:
My input form has a field named 'abstracts.body'
因为它是一个hasMany关系,我需要输入' abstracts.0.body'
然后LeWestopher的其余部分将会有效 - 为我想要从Controller填写的字段添加一个索引,所以$ data [摘要] [0] [' user_id' ] => ... 等等。谢谢!
答案 1 :(得分:3)
您未正确处理您的$ data ['摘要']数组,导致关联无法保存。 $ data ['摘要']预计将是一系列摘要。你的问题在于:
$data['abstracts']['user_id'] = $article->user_id;
$data['abstracts']['approved'] = 0;
您可以通过将此更改为:
来轻松解决此问题foreach($data['abstracts'] as $index => $abstract) {
$abstract['user_id'] = $article->user_id;
$abstract['approved'] = 0;
$data['abstracts'][$index] = $abstract;
}
这应该正确地迭代你的摘要数组,适当地设置user_id和批准的密钥然后它应该保存正确。
CakePHP 3.x Documentation on Saving Associations
编辑:确实非常有趣的问题。不使用patchEntity就尝试一下,而是单独使用newEntity:
public function add()
{
if ($this->request->is('post')) {
$data = $this->request->data;
// Post process abstracts objects
foreach($data['abstracts'] as $index => $abstract) {
$abstract['user_id'] = $article->user_id;
$abstract['approved'] = 0;
$data['abstracts'][$index] = $abstract;
}
// Build newEntity
$article = $this->Articles->newEntity($data, [
'associated' => ['Abstracts']
]);
// Save our entity with associations
if ($this->Articles->save($article, [
'validate' => false,
'associated' => ['Abstracts']
])) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
// On save fail
$this->Flash->error(__('Unable to add your article.'));
$this->set('article', $article);
}
}
编辑2:您的问题看起来确实在您的表单帮助中。您当前的表单帮助程序输入创建一个$ data数组,如下所示:
$data = [
'abstracts' => [
'body' => 'example text'
],
'category' => 'Science'
];
应该是什么样的:
$data = [
'abstracts' => [
['body' => 'example text'],
['body' => 'Im your second abstract'],
['body' => 'Abstract three!']
],
'category' => 'Science'
];
问题出在:
abstracts.body
应该读作(以数组点表示法):
// abstracts.0.body
echo $this->Form->input('abstracts.0.body', [
'label' => 'summary of article',
'maxlength' =>'440',
'rows' => '7'
]);
我认为这应该是你遇到的最后一个问题。