gpt4 book ai didi

cakephp-3.0 - 无法在 cakephp 3.0 中插入新行

转载 作者:行者123 更新时间:2023-12-02 21:04:59 25 4
gpt4 key购买 nike

我无法在表中插入新行并收到错误

2016-04-12 09:23:54 Error: [RuntimeException] Cannot insert row in "table_name" table, it has no primary key. Request URL:

我正在使用 ORM 和下面给出的代码

$entityTable = TableRegistry::get('TableName');
$entity = $entityTable->newEntity();
$entity->name = 'Test Name';
$entity->image = 'test.png';
$entity->type = 1;
if($entityTable->save($entity)) {
$this->Flash->success('Added successfully.');
} else {
$this->Flash->error('Error!.');
}

我的 table 是

CREATE TABLE `table_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` text COLLATE utf8_unicode_ci NOT NULL,
`type` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

最佳答案

添加$this->primaryKey('id');

在 src>>模型>>表>>表名表...在你的初始化方法中

关于cakephp-3.0 - 无法在 cakephp 3.0 中插入新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36569143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com