无法在 cakephp 3.0 中插入新行

Ami*_*mar 2 cakephp-3.0

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

2016-04-12 09:23:54 错误:[RuntimeException] 无法在“table_name”表中插入行,它没有主键。请求网址:

我正在使用 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!.');
} 
Run Code Online (Sandbox Code Playgroud)

我的桌子是

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;
Run Code Online (Sandbox Code Playgroud)

小智 5

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

在 src>>Model>>Table>>TableNamesTable... 在初始化方法中