CakePHP3.0可以';无法保存多个关联数据

CakePHP3.0可以';无法保存多个关联数据,cakephp,model-associations,cakephp-3.0,Cakephp,Model Associations,Cakephp 3.0,我在使用多个关联保存数据时遇到问题 这是我的桌子 1) post表格:每个项目都有一个唯一的id id | title | ... 1 | Aloha | ... 2) 图像表 id | post_id | image | ... 1 | 1 | abc.jpg | ... 2 | 1 | efg.jpg | ... 我的型号(表格) 发布模型 // PostsTable.php <?php namespace App\Model\Table;

我在使用多个关联保存数据时遇到问题

这是我的桌子

1) post表格:每个项目都有一个唯一的id

id | title | ... 
1  | Aloha | ...
2) 图像表

id | post_id | image   | ...  
1  | 1       | abc.jpg | ...
2  | 1       | efg.jpg | ...
我的型号(表格)

发布模型

// 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'
        ]);
     }
}

...
// 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');
     }
}

...
我的模板

(更新) 调试($post)

我不知道我做错了什么

谢谢

试试这个:

<?php echo $this->Form->input('0.Images.image'); ?>
<?php echo $this->Form->input('1.images.image'); ?>
<?php echo $this->Form->input('2.images.image'); ?>


使用int之前,根据

我没有查看所有内容,但我看到您的关联声明中有一个错误:

$this->hasMany('Images', [
    'foreignKey' => 'id'
]);
文件说:

foreignKey:在其他模型中找到的外键的名称。如果需要定义多个关系,这尤其方便。此键的默认值是实际模型的带下划线的单数名称,后缀为“\u id”

因此,它应该是:

$this->hasMany('Images', [
    'foreignKey' => 'post_id'
]);
甚至:

$this->hasMany('Images');

我刚刚完成了3个小时的旅行,除了主模型外,还保存了很多内容。如果考虑使用许多关联项保存许多主模型的对象,这种斗争似乎更糟

  • hasMany关系中的外键是当前模型(实体)的单数名称,后缀为_id,如下所示:

    class MainModel extends Table {
    ...
    public function initialize(array $config) {
    ...
    // remember the "s" at the end of the name
    $this->hasMany('VeryWeirdCalleds', [
            'className' => 'VeryWeirdCalleds',
            'foreignKey' => 'main_model_id',
            'propertyName' => 'very_weird_calleds'
        ]);
    ...
    }
    ...
    }
    
  • 然后,在主实体中设置accessible,以便主模型可以基于“very_-wird_-calleds”索引保存关联:

    最后(但并非最不重要的一点):控制器保存。这通常是最难克服的部分,因为文档没有详细说明整个过程:

    class MainModelsController extends AppController {
    
    public function add($data) {
            $data = [
                [
                    'name' => 'Hello', 
                    'body' => 'Bla bla', 
                    'very_weird_calleds' => [
                        [
                            'name' => 'Very Weird Called'
                        ]
                    ]
                ]
            ];
            foreach ($data as $record) {
                $main = $this->MainModels->newEntity($record);
    
                if(isset($record['images']) && !empty($record['images'])) {
                    foreach($record['images'] as $record_image) {
                        $image = $this->MainModels->VeryWeirdCalleds->newEntity();
                        $image->IMAGE = $record_image['IMAGE'];
                        $import->very_weird_calleds[] = $image;
                    }
                }
                if (!$this->MainModels->save($main)) {
                    $this->Flash->error('The main model could not be saved. Please, try again.');
                }
            }
        }
    
    解释?首先,我们对数据进行循环,之前准备的数据如下:

    ['main','model','data','association\u accessible\u property'=>['associated\u data']]

    然后,我们使用与主模型相同的方法为相关数据创建新条目。最后一件事是将那些关联的实体添加到主模型实体中


    请特别注意本例中给出的名称。“s”-es和camelcase不是巧合。

    “无法保存…”的确切含义是什么?还有什么是
    debug($post)显示?@ndm I表示$this->Post->save($Post,['associated'=>'Images'])的值始终返回false。我已经更新了我的问题,并显示了调试($post)的结果。嗯,实体看起来不错,保存后是什么样子的?有错误吗?@ndm没有,只需显示添加表单,并显示错误消息“无法保存帖子。请重试。”。我认为在保存图像关系时,story_id是空的。答案不应该只是代码,你应该能够解释为什么/如何解决这个问题(这很可能不是,因为小写字母是正确的,除非自定义)。根据
    
    $this->hasMany('Images', [
        'foreignKey' => 'post_id'
    ]);
    
    $this->hasMany('Images');
    
    class MainModel extends Table {
    ...
    public function initialize(array $config) {
    ...
    // remember the "s" at the end of the name
    $this->hasMany('VeryWeirdCalleds', [
            'className' => 'VeryWeirdCalleds',
            'foreignKey' => 'main_model_id',
            'propertyName' => 'very_weird_calleds'
        ]);
    ...
    }
    ...
    }
    
    class MainModel extends Entity {
        protected $_accessible = [
            ...
            'very_weird_calleds' => true,
        ];
    }
    
    class MainModelsController extends AppController {
    
    public function add($data) {
            $data = [
                [
                    'name' => 'Hello', 
                    'body' => 'Bla bla', 
                    'very_weird_calleds' => [
                        [
                            'name' => 'Very Weird Called'
                        ]
                    ]
                ]
            ];
            foreach ($data as $record) {
                $main = $this->MainModels->newEntity($record);
    
                if(isset($record['images']) && !empty($record['images'])) {
                    foreach($record['images'] as $record_image) {
                        $image = $this->MainModels->VeryWeirdCalleds->newEntity();
                        $image->IMAGE = $record_image['IMAGE'];
                        $import->very_weird_calleds[] = $image;
                    }
                }
                if (!$this->MainModels->save($main)) {
                    $this->Flash->error('The main model could not be saved. Please, try again.');
                }
            }
        }