如何使用 Phalcon 获取最后插入的 ID?
How to get last insert id with Phalcon?
我在获取自动增量主键列的最后一个 ID 时尝试使用 LAST_INSERT_ID()
,但我得到 EOF 异常:
function add($tab) {
$champs= "";
$value = "";
$separateur ="";
$tab["commande_date"] = convertDateFormat5($tab["commande_date"]);
foreach ($tab as $k => $v){
if ($k == "salle_code" || $k == "table_code")
continue;
$champs .= $separateur . $k;
$value .= $separateur . "'" . $v . "'";
$separateur = ",";
}
$champs = '('.$champs.')';
$value = '('.$value.')';
$sSQL = "
INSERT INTO Commande $champs
VALUES $value
";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$sSQL = "SELECT LAST_INSERT_ID() as last_id";
$queryId = new Query($sSQL,$this->getDI());
return $queryId->execute();
}
那么如何使用 Phalcon
获取最后一个 ID?
你可以使用lastInsertId()
函数
$sSQL = "INSERT INTO Commande $champs VALUES $value ";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$lastId = $query->lastInsertId();
Update
这里是一个插入和取回 lastinsert id 的例子。
$success = $connection->insert(
"robots",
array("Astro Boy", 1952),
array("name", "year")
);
//Getting the generated id
$id = $connection->lastInsertId();
我认为这可能对您有所帮助。
您处理此问题的方式相当反 MVC,并且您在 table 上保留了很多 Phalcon 功能。你应该有一个名为 Commande 的数据库模型,它应该像这样创建:
$comande = new Commande();
然后您可以分配如下值:
$comande->field1 = "hello";
$comande->adifferentfield = "world";
...
然后你会跟进:
$comande->save(); //yes I am aware we should test for success
然后您可以访问您的关键字段,该字段将自动填充,例如:
print $comande->id;
你错过了很多错误处理和验证的方式。
保存时在控制器中:
if ($client->save() == false) {
foreach ($client->getMessages() as $message) {
$this->flash->error($message);
}
return $this->forward('clients/new');
}
// Get new client ID and redirect to new client
$this->response->redirect("client/page/$client->id");
$this->view->disable();
$yourModel = new YourModel();
$yourModel->save() // Or create();
$newId = $yourModel->getWriteConnection()->lastInsertId();
我觉得这样比较容易。希望对你有帮助。
我在获取自动增量主键列的最后一个 ID 时尝试使用 LAST_INSERT_ID()
,但我得到 EOF 异常:
function add($tab) {
$champs= "";
$value = "";
$separateur ="";
$tab["commande_date"] = convertDateFormat5($tab["commande_date"]);
foreach ($tab as $k => $v){
if ($k == "salle_code" || $k == "table_code")
continue;
$champs .= $separateur . $k;
$value .= $separateur . "'" . $v . "'";
$separateur = ",";
}
$champs = '('.$champs.')';
$value = '('.$value.')';
$sSQL = "
INSERT INTO Commande $champs
VALUES $value
";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$sSQL = "SELECT LAST_INSERT_ID() as last_id";
$queryId = new Query($sSQL,$this->getDI());
return $queryId->execute();
}
那么如何使用 Phalcon
获取最后一个 ID?
你可以使用lastInsertId()
函数
$sSQL = "INSERT INTO Commande $champs VALUES $value ";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$lastId = $query->lastInsertId();
Update
这里是一个插入和取回 lastinsert id 的例子。
$success = $connection->insert(
"robots",
array("Astro Boy", 1952),
array("name", "year")
);
//Getting the generated id
$id = $connection->lastInsertId();
我认为这可能对您有所帮助。
您处理此问题的方式相当反 MVC,并且您在 table 上保留了很多 Phalcon 功能。你应该有一个名为 Commande 的数据库模型,它应该像这样创建:
$comande = new Commande();
然后您可以分配如下值:
$comande->field1 = "hello";
$comande->adifferentfield = "world";
...
然后你会跟进:
$comande->save(); //yes I am aware we should test for success
然后您可以访问您的关键字段,该字段将自动填充,例如:
print $comande->id;
你错过了很多错误处理和验证的方式。
保存时在控制器中:
if ($client->save() == false) {
foreach ($client->getMessages() as $message) {
$this->flash->error($message);
}
return $this->forward('clients/new');
}
// Get new client ID and redirect to new client
$this->response->redirect("client/page/$client->id");
$this->view->disable();
$yourModel = new YourModel();
$yourModel->save() // Or create();
$newId = $yourModel->getWriteConnection()->lastInsertId();
我觉得这样比较容易。希望对你有帮助。