如何在Phalcon中获取最后插入的id?

4
尝试使用 LAST_INSERT_ID() 获取自增主键列的最后一个 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呢?

尝试 $query->insert_id - urfusion
你试过这个吗?我几年前用过lastInsertId() - urfusion
你写了 insert_id !!!! - pheromix
是的,那是我的先前分析。 :) 现在检查 lastInsertId() 是否正常工作? - urfusion
try to var_dump($ret). - urfusion
4个回答

12

你处理这个问题的方式相当反MVC,并且浪费了很多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;

如果你按照现在的方式处理,那么你会错过很多错误处理和验证。


2
该死,Phalcon 真的非常棒! - Patrick Manser
抱歉,我使用的是Phalcon 3.4版本,当我调用$object->id(在$object->save()之后)时,它返回0。请问如何修复这个问题,谢谢。 - Phu Sanh

5
$yourModel = new YourModel();
$yourModel->save() // Or create();
$newId = $yourModel->getWriteConnection()->lastInsertId();

我认为这种方式更容易。希望有所帮助。

3
您可以使用lastInsertId()函数。
$sSQL = "INSERT INTO Commande $champs VALUES $value ";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$lastId = $query->lastInsertId();

更新

以下是插入和获取最后插入ID的例子。

$success = $connection->insert(
     "robots",
     array("Astro Boy", 1952),
     array("name", "year")
 );

 //Getting the generated id
 $id = $connection->lastInsertId();

我认为这可能对你有所帮助。

致命错误:Call to undefined method Phalcon\Mvc\Model\Query::lastInsertId():即使使用 $query->lastInsertId()或 $ret->lastInsertId(),也是如此。 - pheromix
@pheromix:我正在检查PDO类http://phalcon.agent-j.ru/en/1.2.6/Phalcon/Db/Adapter/Pdo,并且有一个方法http://phalcon.agent-j.ru/en/1.2.6/Phalcon/Db/Adapter/Pdo#lastInsertId-details。你可能漏掉了什么。 - urfusion
这里是最新的https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo_Mysql.html,其中还包含lastinsertid函数。 - urfusion
谢谢你提供的链接 :) 跟随他们的示例后,现在它已经可以工作了。 - pheromix
很高兴知道这个消息。 :) - urfusion
但是您应该根据文档更新您的答案 :) - pheromix

0

在控制器中保存时:

   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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接