We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

lastInsertId() / POSTGRES

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



17.7k
edited May '15

try this


$this->db->lastInsertId()


11.6k

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



17.7k

You can display the code ?



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



17.7k

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