lastInsertId() / POSTGRES

Hi, Does 'getConnection()->lastInsertId();' is supposed to work using Postgresql database?



9.0k
edited May '15

try this

$this->db->lastInsertId()

nop, it's doesn't work too..



9.0k

You can display the code ?

edited May '15

controller function:

```php

public function changeCommercialAction()
{
    if ($this->request->isPost()) {
        if ($this->request->isAjax()) {
            $nc = $this->request->getPost('nc');
            $id = $this->request->getPost('id');
            $commercial = Commerciaux::findFirst("nomCommercial = '$nc'");
            if (!$commercial) {
                $messageCommercial = "commercial not found";
            } else {
                $messageCommercial = "commercial saved";
                $commercialID = $commercial->id;
                $Newdevi = Devis::findFirstByid($id);
                $Newdevi->commercialID = $commercialID;

                if ($Newdevi->update() == false) {
                    $messageDevis = "le commercial n'a pas été mis à jour";
                }
                else {
                    $lastInsertId = $this->db->lastInsertId();
                    $messageDevis = 'lastInsertId :' . $lastInsertId;
                }
            }
            $this->view->messageCommercial = $messageCommercial;
            $this->view->messageDevis = $messageDevis;
        }
    }
}

```

ajax response:

{"title":"Devis","messageCommercial":"commercial saved","messageDevis":"lastInsertId :","success":true,"message":"blurp"}



9.0k

Mmm... i think that the lastInsertId work is when you are inserting data... sorry

the return of lastInsertId is -1

why not do another query with the last descendant ordered by id?

You need to pass the name of the PostgreSQL sequence as parameter of lastInsertId