lastInsertId() / POSTGRES

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



13.6k
edited May '15

try this


$this->db->lastInsertId()

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



13.6k

You can display the code ?

edited May '15

controller function:


    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"}



13.6k

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