CakePHP 3.0无法保存hasMany关联的数据 [英] CakePHP 3.0 Can't save hasMany associated data
问题描述
使用hasMany关联保存数据时出现问题
这是我的表格
1)表:每个项目都有一个唯一的ID。
id |标题| ...
1 | Aloha | ...
2)图片表
id | post_id |图像| ...
1 | 1 | abc.jpg | ...
2 | 1 | efg.jpg | ...
我的模型
/ / PostsTable.php
<?php
命名空间App\Model\Table;
使用Cake\ORM\Query;
使用Cake\ORM \Table;
使用Cake\Validation\Validator;
class PostTable extends Table {
public function initialize(array $ config){
$ this-> table('posts');
$ this-> displayField('title');
$ this-> primaryKey('id');
$ this-> addBehavior('Timestamp');
$ this-> hasMany('Images',[
'foreignKey'=>'id'
]);
}
}
...
图片模型
: // ImagesTable.php
<?php
namespace App\Model\Table;
使用Cake\ORM\Query;
使用Cake\ORM \Table;
使用Cake\Validation\Validator;
class ImagesTable extends Table {
public function initialize(array $ config){
$ this-> table('images');
$ this-> displayField('id');
$ this-> primaryKey('id');
$ this-> addBehavior('Timestamp');
$ this-> belongsTo('Posts');
}
}
...
我的控制器
// PostsController.php
。 ..
public function add(){
$ post = $ this-> Posts-> newEntity($ this-> request-> data,[
'associated' > ['Images']
]);
if($ this-> request-> is('post')){
if($ this-> Posts-> save '=> ['Images']])){
$ this-> Flash-> success('帖子已保存。
return $ this-> redirect(['action'=>'index']);
} else {
$ this-> Flash->错误('无法保存帖子,请重试。
}
}
$ this-> set('post',$ post);
}
...
我的模板
// add.ctp
<?= $ this - > Form-> create($ post); ?>
<?php echo $ this-> Form-> input('title'); ?>
<?php echo $ this-> Form-> input('images.0.image'); ?>
<?php echo $ this-> Form-> input('images.1.image'); ?>
<?php echo $ this-> Form-> input('images.2.image'); ?>
<?= $ this-> Form->按钮(__('Submit'),['class'=>'button-green'])?
<?= $ this-> Form-> end()?>
输入数组结果调试 >
[
'title'=> 'Hello',
'images'=> [
(int)0 => [
'image'=> 'testa.jpeg'
],
(int)1 => [
'image'=> 'testb.jpeg'
],
(int)2 => [
'image'=> 'testc.jpeg'
]
]
]
更新)
debug($ post)(App\Model\Entity\Story){
'new'=> true,
'accessible'=> [
'title'=> true,
'images'=> true
],
'properties'=> [
'title'=> 'Hello',
'images'=> [
(int)0 =>对象(App\Model\Entity\Image){
'new'=>真,
'accessible'=> [
'post_id'=> true,
'image'=> true,
'post'=> true
],
'properties'=> [
'image'=> 'testa.jpeg'
],
'dirty'=> [
'image'=> true
],
'original'=> [],
'virtual'=> [],
'errors'=> [],
'repository'=> 'Images'
},
(int)1 =>对象(App\Model\Entity\Image){
'new'=> true,
'accessible'=> [
'post_id' => true,
'image'=> true,
'post'=> true
],
'properties'=> [
'image'=> 'testb.jpeg'
],
'dirty'=> [
'image'=> true
],
'original'=> [],
'virtual'=> [],
'errors'=> [],
'repository'=> 'images'
},
(int)2 =>对象(App\Model\Entity\Image){
'new'=> true,
'accessible'=> [
'post_id'=> true,
'image'=>真,
'post'=> true
],
'properties'=> [
'image'=> 'testc.jpeg'
],
'dirty'=> [
'image'=> true
],
'original'=> [],
'virtual'=> [],
'errors'=> [],
'repository'=> 'Images'
}
]
],
'dirty'=> [
'title'=> true,
'images'=> true
],
'original'=> [],
'virtual'=> [],
'errors'=> [],
'repository'=> 'Stories'
}
我无法弄清楚做错了
感谢
解决方案但我看到您的关联声明中有错误:
$ this-> hasMany('Images',[
'foreignKey'=>'id'
]);
文档说:
foreignKey:在其他模型中找到的外键的名称。如果您需要定义多个hasMany关系,这是特别方便。此键的默认值是实际模型的下划线单数名称,后缀为_id。
:
$ this-> hasMany('Images',[
'foreignKey'=>'post_id'
]);
或甚至:
$ this-> hasMany('Images');
I've problem when saving data with hasMany association
This is my table
1) post table: each item has an unique id.
id | title | ... 1 | Aloha | ...
2) images table
id | post_id | image | ... 1 | 1 | abc.jpg | ... 2 | 1 | efg.jpg | ...
My Model (Table)
Posts Model
// PostsTable.php <?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\Table; use Cake\Validation\Validator; class PostsTable extends Table { public function initialize(array $config) { $this->table('posts'); $this->displayField('title'); $this->primaryKey('id'); $this->addBehavior('Timestamp'); $this->hasMany('Images', [ 'foreignKey' => 'id' ]); } } ...
Images Model
// ImagesTable.php <?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\Table; use Cake\Validation\Validator; class ImagesTable extends Table { public function initialize(array $config) { $this->table('images'); $this->displayField('id'); $this->primaryKey('id'); $this->addBehavior('Timestamp'); $this->belongsTo('Posts'); } } ...
My Controller
// PostsController.php ... public function add() { $post = $this->Posts->newEntity($this->request->data, [ 'associated' => ['Images'] ]); if ($this->request->is('post')) { if ($this->Posts->save($post, ['associated' => ['Images']])) { $this->Flash->success('The post has been saved.'); return $this->redirect(['action' => 'index']); } else { $this->Flash->error('The post could not be saved. Please, try again.'); } } $this->set('post', $post); } ...
My Template
// add.ctp <?= $this->Form->create($post); ?> <?php echo $this->Form->input('title'); ?> <?php echo $this->Form->input('images.0.image'); ?> <?php echo $this->Form->input('images.1.image'); ?> <?php echo $this->Form->input('images.2.image'); ?> <?= $this->Form->button(__('Submit'), ['class' => 'button-green']) ?> <?= $this->Form->end() ?>
Input array result Debug
[ 'title' => 'Hello', 'images' => [ (int) 0 => [ 'image' => 'testa.jpeg' ], (int) 1 => [ 'image' => 'testb.jpeg' ], (int) 2 => [ 'image' => 'testc.jpeg' ] ] ]
(Update) debug($post)
object(App\Model\Entity\Story) { 'new' => true, 'accessible' => [ 'title' => true, 'images' => true ], 'properties' => [ 'title' => 'Hello', 'images' => [ (int) 0 => object(App\Model\Entity\Image) { 'new' => true, 'accessible' => [ 'post_id' => true, 'image' => true, 'post' => true ], 'properties' => [ 'image' => 'testa.jpeg' ], 'dirty' => [ 'image' => true ], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Images' }, (int) 1 => object(App\Model\Entity\Image) { 'new' => true, 'accessible' => [ 'post_id' => true, 'image' => true, 'post' => true ], 'properties' => [ 'image' => 'testb.jpeg' ], 'dirty' => [ 'image' => true ], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Images' }, (int) 2 => object(App\Model\Entity\Image) { 'new' => true, 'accessible' => [ 'post_id' => true, 'image' => true, 'post' => true ], 'properties' => [ 'image' => 'testc.jpeg' ], 'dirty' => [ 'image' => true ], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Images' } ] ], 'dirty' => [ 'title' => true, 'images' => true ], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Stories' }
I can't figure out what I am doing wrong
Thanks
解决方案I haven't looked everything, but I saw there is an error in your association declaration:
$this->hasMany('Images', [ 'foreignKey' => 'id' ]);
The docs say:
foreignKey: the name of the foreign key found in the other model. This is especially handy if you need to define multiple hasMany relationships. The default value for this key is the underscored, singular name of the actual model, suffixed with ‘_id’.
So it should be:
$this->hasMany('Images', [ 'foreignKey' => 'post_id' ]);
or even:
$this->hasMany('Images');
这篇关于CakePHP 3.0无法保存hasMany关联的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!