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

Query insert id

How can i grab the insert id after insert query ?

    $query = $this->modelsManager->executeQuery("INSERT INTO CRM\Models\Services (name,text) VALUES (:name:, :text:) ", [
                    'name'  => $this->request->getPost("name"),
                    'text'  => $this->request->getPost("text"),
                ]);

This way:

$result = $this->modelsManager->executeQuery("INSERT INTO CRM\Models\Services (name,text) VALUES (:name:, :text:) ", [
    'name'  => $this->request->getPost("name"),
    'text'  => $this->request->getPost("text"),
]);
if (!$result->success()) {
    foreach ($result->getMessages() as $message) {
        echo $message;
    }
} else {
     $model = $result->getModel(); // instance of CRM\Models\Services populated with inserted values
    echo $model->getId(); //Access the id field
}

Got it !

$model = $result->getModel();
$model->_ // where _ is my main id field