保存与 Cakephp 3 的关联

2023-12-05

我在使用 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']
        ]);
    }
}  

那是我的模型:

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

这是我的数据库:

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;

我的表格看起来像这样:

<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]"> 

我想创建一个包含所有关联的表单,并通过一个请求保存它。


缺少列

没有名为的列heading在你的articles table.

外键配置无效

你的配置看起来有问题,targetForeignKey is for BelongsToMany协会,以及article_id(另一个表的主键)是not中的外键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'
        ]);   
    } 
} 

如果您坚持约定并命名主键,您可以避免很多混乱id.

See also

  • Cookbook > 模型 > 表对象 > 基本用法
  • Cookbook > 模型 > 表对象 > HasMany 关联

可能缺少辅助功能配置

由于您正在尝试保存非默认字段(即Topic数据而不是Topic主键),您必须通过以下方式使该字段可访问以进行批量分配newEntity。例如,这可以通过Entitiy::$_accessible属性,该属性对于该实体的每个实例都是永久的,直到在运行时被覆盖

class Article extends Entity {
    protected $_accessible = [
        // ...
        'topic' => true
    ];
}

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

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

See also

  • Cookbook > 模型 > 实体 > 质量分配
  • API > Cake\ORM\Table::newEntity()

字段命名约定

最后,您的表单数据格式不正确,名称默认应为小写并带下划线,以便与实体属性匹配,并且它们应该是单数(除非它是hasMany or belongsToMany关联),即应该是article and topic代替Articles and Topics,实际上也没有必要使用article at all.

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

也可以看看Cookbook > Helpers > FormHelper > 字段命名约定

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

保存与 Cakephp 3 的关联 的相关文章