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

标签 cakephp-3.0

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

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/

相关文章:

php - 如何使用 CakePHP 表保存方法保存带有 "/"的 fcolumn?

mysql - 一对多 CakePHP3(通过匹配表)

php - CakePHP 3 显示 DebugKit 面板

php - Cakephp 3在父类的beforeFilter中重定向

cakephp - 如何强制 CakePHP 3 和 CakePHP 4 显示弃用警告/通知?

php - 如何在创建蛋糕烘焙模板 cakephp 3 时跳过选定的表格列

error-handling - Cakephp 3-抛出异常时未传输var以查看

CakePHP 3.x : Cache paginated search

cakephp-3.0 - Router::scope 和 Router::prefix 之间的 cakephp 区别

php - 我们如何在 Cakephp 中使用 SoapClient...?