我无法在表格中插入新行并收到错误
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!.');
}
我的桌子是
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;
答案 0 :(得分:3)
添加function getUserId() {
getArrayFromDatabase(function(err, result) {
console.log(result);
});
}
function getArrayFromDatabase(callback) {
var arrUser = new Array();
connection.query('SELECT iUserId,sFromEmailAddress FROM tblFromSenderList',function (err, results, fields) {
if (err) {
return callback(err, null);
}
if (results.length > 0) {
for (var i = 0; i< results.length ;i++){
var reader = results[i];
arrUser[reader['sFromEmailAddress']] = reader['iUserId'];
}
console.log(arrUser); // prints value over here
callback(null,arrUser);
}
});
}
in src&gt;&gt; Model&gt;&gt; Table&gt;&gt; TableNamesTable ... 在你的初始化方法