2016-04-12 31 views
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:

我使用下面

$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!.'); 
} 

給出ORM和我的代碼而我的表是

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; 
+1

add $ this-> primaryKey('id');在src >>模型>>表>> TableNamesTable ...在你的初始化方法 – prats1411

+0

它現在工作正常。謝謝@ prats1411 –

+0

aaah ..答案現在發佈了,它在一個選項卡中丟失了..很高興它正在工作,請將其標記爲正確:)謝謝! – prats1411

回答

2

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

in src >> Model >> Table >> TableNamesTable ... in your initialize method