Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Phalcon获取最后一个插入id?_Php_Mysql_Phalcon - Fatal编程技术网

Php 如何使用Phalcon获取最后一个插入id?

Php 如何使用Phalcon获取最后一个插入id?,php,mysql,phalcon,Php,Mysql,Phalcon,在获取autoincrement主键列的最后一个ID时,我尝试使用LAST\u INSERT\u ID(),但出现EOF异常: function add($tab) { $champs= ""; $value = ""; $separateur =""; $tab["commande_date"] = convertDateFormat5($tab["commande_date"]); foreach ($ta

在获取autoincrement主键列的最后一个ID时,我尝试使用
LAST\u INSERT\u 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();
更新

下面是一个插入并获取lastinsert id的示例

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

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

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

你处理这件事的方式相当反MVC,你把很多Phalcon功能放在桌面上。您应该有一个名为Commande的数据库模型,该模型应该创建如下内容:

$comande = new Commande();
print $comande->id;
然后可以指定如下值:

$comande->field1 = "hello";
$comande->adifferentfield = "world";
...
然后,您将继续:

$comande->save();  //yes I am aware we should test for success
然后,您可以访问密钥字段,该字段将自动填充,例如:

$comande = new Commande();
print $comande->id;

您丢失了大量的错误处理和验证工作。保存时,请在controller中执行以下操作:

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

我认为这样比较容易。希望对您有所帮助。

试试$query->insert\u id您试过这个吗。几年前我就用过了
lastInsertId()
你写的insert\u id!!!!是的,这是我之前的分析。:)现在检查
lastInsertId()
是否有效?尝试var\u dump($ret)。否<代码>致命错误:调用未定义的方法Phalcon\Mvc\Model\Query::lastInsertId():即使使用$Query->lastInsertId()或$ret->lastInsertId()@pheromix,我也在检查PDO类。还有一种方法。您可能遗漏了一些内容。以下是最新版本,其中还包含lastinsertid功能。感谢您提供的链接:)在遵循他们的示例后,它现在可以正常工作。很高兴知道这一点。:)该死,法尔康太棒了!对不起,我使用的是phalcon 3.4,当我调用$object->id(在$object->save()之后)时,它返回0。我怎样才能修好它,谢谢