我的CakePHP 3.0应用程序使用一个MySQL数据库,该数据库目前有两个具有相同结构(但数据不同)的表。我想合并这两个表,但要跟踪数据,因为它有关联。
因此,我认为我只需要从一个表中读取记录,并将它们复制到另一个表中,并使用一个标志来指示它们来自旧表,如下所示:
public function copyarchive() {
$oldalbums= TableRegistry::get('Archivealbums');
$newalbums= TableRegistry::get('Albums');
$albumlist = $oldalbums->find(); // Get all archived albums
foreach($albumlist as $oldalbum) {
// Copy album details to album table and get new id
$newalbum = $newalbums->newEntity([
'is_archive' => 1,
'name' => $oldalbum->name,
'date' => $oldalbum->date,
'description' => $oldalbum->description,
'order' => $oldalbum->order,
]);
if ($newalbums->save($newalbum)) {
$id = $newalbum->id;
// ... now do other things ...
}
}
}
我得到以下错误:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order) VALUES (1, 'Pre-1920\'s', '2013-10-22 23:00:00', '<p>\r\n Photos from bef' at line 1
SQL查询如下所示:
INSERT INTO albums (is_archive, name, date, description, order) VALUES (:c0, :c1, :c2, :c3, :c4)
它还建议“这可能是由使用自动表格引起的吗?”
我是不是做了什么傻事?
发布于 2018-03-15 02:46:42
order是一个受MySQL保护的单词,用于排序结果。我可以使用不同的列名,也可以在代码中使用反引号:
public function copyarchive() {
$oldalbums= TableRegistry::get('Archivealbums');
$newalbums= TableRegistry::get('Albums');
$albumlist = $oldalbums->find(); // Get all archived albums
foreach($albumlist as $oldalbum) {
// Copy album details to album table and get new id
$newalbum = $newalbums->newEntity([
'is_archive' => 1,
'name' => $oldalbum->name,
'date' => $oldalbum->date,
'description' => $oldalbum->description,
'`order`' => $oldalbum->order,
]);
if ($newalbums->save($newalbum)) {
$id = $newalbum->id;
// ... now do other things ...
}
}
}
SQL错误确实告诉您问题所在。通常,不要在您的模式中使用MySQL保护字。
https://stackoverflow.com/questions/49285363
复制相似问题