4

I tried using LAST_INSERT_ID() when getting the last id of the autoincrement primary key column but I get EOF exception :

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

    }

So how to get the last id with Phalcon ?

pheromix
  • 18,213
  • 29
  • 88
  • 158

4 Answers4

12

The way you are handling this is fairly anti-MVC and you leave a lot of the Phalcon functionality on the table. You should have a database model named Commande which should be created something like:

$comande = new Commande();

then you can assign values like:

$comande->field1 = "hello";
$comande->adifferentfield = "world";
...

You'd then follow that up with:

$comande->save();  //yes I am aware we should test for success

Then you could access your key field, which would be populated automatically, something like:

print $comande->id;

You're missing out on a lot of the error handling and validation going about it the way you are.

Jesse Q
  • 1,451
  • 2
  • 13
  • 18
5
$yourModel = new YourModel();
$yourModel->save() // Or create();
$newId = $yourModel->getWriteConnection()->lastInsertId();

I think it's easier in this way. Hope it's helpful.

Nhan
  • 3,595
  • 6
  • 30
  • 38
X.Herry
  • 65
  • 1
  • 3
3

you can use the lastInsertId() function

$sSQL = "INSERT INTO Commande $champs VALUES $value ";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
$lastId = $query->lastInsertId();

Update

Here is an example of inserting and getting back the lastinsert id.

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

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

I think this might help you.

urfusion
  • 5,528
  • 5
  • 50
  • 87
  • no ! `Fatal error: Call to undefined method Phalcon\Mvc\Model\Query::lastInsertId()` : even with $query->lastInsertId() or $ret->lastInsertId() – pheromix Sep 23 '15 at 14:16
  • @pheromix : I am checking the PDO class http://phalcon.agent-j.ru/en/1.2.6/Phalcon/Db/Adapter/Pdo. and there is a method http://phalcon.agent-j.ru/en/1.2.6/Phalcon/Db/Adapter/Pdo#lastInsertId-details. You might be missing something. – urfusion Sep 23 '15 at 14:22
  • here is the latest https://docs.phalconphp.com/en/latest/api/Phalcon_Db_Adapter_Pdo_Mysql.html which also have lastinsertid function – urfusion Sep 24 '15 at 06:46
  • thank you for the link :) now it works after following their example. – pheromix Sep 24 '15 at 07:13
  • Glad to know that. :) – urfusion Sep 24 '15 at 07:14
  • but you should update your answer accordingly to the doc :) – pheromix Sep 24 '15 at 07:14
0

In controller when you save:

   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();
Viktor Bogutskii
  • 870
  • 1
  • 14
  • 22