保存与Cakephp 3的关联

bas*_*tey 2 cakephp cakephp-3.0

我遇到CakePHP 3的问题,并保存新实体及其与一个操作的关联.

在我看来,我这样做就像文档中推荐的那样.

我的控制器:

$articles = TableRegistry::get('Articles');       
$article = $articles->newEntity($this->request->data);  

if ($this->request->is('post')) {            

    $valid = $articles->validate($article, [
        'associated' => ['Topics']
    ]);

    if ( $valid ) {
        $articles->save($article, [
            'validate' => false,
            'associated' => ['Topics']
        ]);
    }
}  
Run Code Online (Sandbox Code Playgroud)

那是我的模特:

class ArticlesTable extends Table {   
    public function initialize(array $config) {        
        $this->primaryKey('article_id');
        $this->belongsTo ( 'Topics', [
            'targetForeignKey'  => 'topic_id'
        ]);
    }
}

class TopicsTable extends Table {  
    public function initialize(array $config) {        
        $this->primaryKey('topic_id');  
        $this->hasMany ( 'Articles', [
            'targetForeignKey'  => 'article_id'
        ]);    
}  
Run Code Online (Sandbox Code Playgroud)

这是我的数据库:

CREATE TABLE `articles` (
  `article_id` int(11) NOT NULL AUTO_INCREMENT,
  `topic_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`article_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

CREATE TABLE `topics` (
  `topic_id` int(11) NOT NULL AUTO_INCREMENT,
  `topic_title` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`topic_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

我的表格看起来像这样:

<input type="text" id="articles-heading" maxlength="45" required="required" name="Articles[heading]">
<input type="text" id="articles-topics-topic-title" list="topics" name="Articles[Topics][topic_title]"> 
Run Code Online (Sandbox Code Playgroud)

我想创建一个包含所有关联的表单,并将其保存为一个请求.

ndm*_*ndm 10

缺少列

heading您的articles表中没有命名列.

外键配置无效

您的配置看起来是错误的,targetForeignKey用于BelongsToMany关联,并且article_id(另一个表的主键)不是Topic hasMany Comment关联中的外键,它是topic_id,并且在您遵循命名约定时没有必要指定它.

因此,在完全指定外键时,它应该如下所示:

class ArticlesTable extends Table {   
    public function initialize(array $config) {        
        $this->primaryKey('article_id');
        $this->belongsTo ( 'Topics', [
            'foreignKey' => 'topic_id'
        ]);
    }
}

class TopicsTable extends Table {  
    public function initialize(array $config) {        
        $this->primaryKey('topic_id');  
        $this->hasMany ( 'Articles', [
            'foreignKey' => 'topic_id'
        ]);   
    } 
} 
Run Code Online (Sandbox Code Playgroud)

如果你坚持惯例并命名你的主键,你可以避免很多混乱id.

也可以看看

可能缺少可访问性配置

由于您尝试保存非默认字段(即Topic数据而不是Topic主键),因此您必须使该字段可用于批量分配newEntity.这可以通过Entitiy::$_accessible属性实现,该属性对于该实体的每个单个实例都是永久的,直到在运行时覆盖

class Article extends Entity {
    protected $_accessible = [
        // ...
        'topic' => true
    ];
}
Run Code Online (Sandbox Code Playgroud)

或者使用accessibleFields选项Table::newEntity(),仅适用于该单个实例.

$article = $articles->newEntity($this->request->data, [
    'accessibleFields' => [
        'topic' => true
    ]
]);  
Run Code Online (Sandbox Code Playgroud)

也可以看看

现场命名约定

最后你的表单数据格式不正确,名称默认为小写和下划线以匹配实体属性,它们应该是单数(除非它是一个hasManybelongsToMany关联),即它应该是articletopic不是,Articles而且Topics它实际上不是必须使用article.

echo $this->Form->input('heading');
echo $this->Form->input('topic.topic_title');
Run Code Online (Sandbox Code Playgroud)
<input type="text" name="heading">
<input type="text" name="topic[topic_title]"> 
Run Code Online (Sandbox Code Playgroud)

另请参阅Cookbook> Helpers> FormHelper>字段命名约定