小编rsl*_*dyt的帖子

CakePHP 3.0无法保存hasMany关联数据

使用hasMany关联保存数据时遇到问题

这是我的桌子

1)邮政表:每个项目都有一个唯一的ID.

id | title | ... 
1  | Aloha | ...
Run Code Online (Sandbox Code Playgroud)

2)图像表

id | post_id | image   | ...  
1  | 1       | abc.jpg | ...
2  | 1       | efg.jpg | ...
Run Code Online (Sandbox Code Playgroud)

我的模型(表)

帖子模型

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

...
Run Code Online (Sandbox Code Playgroud)

图像模型

// ImagesTable.php
<?php

namespace App\Model\Table;

use Cake\ORM\Query;
use …
Run Code Online (Sandbox Code Playgroud)

cakephp model-associations cakephp-3.0

5
推荐指数
1
解决办法
5415
查看次数

标签 统计

cakephp ×1

cakephp-3.0 ×1

model-associations ×1