Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

how get ID from model->create()

has not found in documentation how get ID record after add it in table

that on similar

function id(){ return mysqli_insert_id($this->conect); }

in

$user = new Users();
$user->password = 'pass';
$user->name = 'user';

if ($user->create())
{
    $id = $user->id(); // ????? how ????
}

You can get it from the connection object like this:

$result = $user->create(); 
if ($result) {
    $id = $user->getConnection()->lastInsertId(); 
}


82.8k
Accepted
answer

If the field 'id' is auto_increment you can automatically get the generated value by just accessing the property after saving:

if ($robot->save()) {
    echo "The robot id is", $robot->id, '<br>';
}


10.0k

thank you for answer

edited Oct '14

Thank you so much! Very clear and legible :D

smooth and easy as it intended to be in Phalcon

If the field 'id' is auto_increment you can automatically get the generated value by just accessing the property after saving:

```php if ($robot->save()) { echo "The robot id is", $robot->id, '<br>'; } ```