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

how to allow $id to auto increment

I am trying to insert user details in db....and it has $id value which should be auto incremented and it is not shown in the form....but i am not able to auto increment the id value becoz of this i need to provide a field for id in view part...which should be avoided......Can anyone help me??

below is my code...

        $user->id = $this->request->getPost('id');
        $user->name = $this->request->getPost('name');
        $user->load = $this->request->getPost('load');
        $user->save();
edited Oct '16

You can't change primiary keys in phalcon. Just do:

$user = User::findFirst($this->request->getPost('id'));
$user->name = $this->request->getPost('name');
$user->load = $this->request->getPost('load');
$user->save();

I don't really understand. Auto increment is mysql setting, not framework, phalcon has nothing to do with this. If you want to add new user just do:

$user = new User();
$user->name = $this->request->getPost('name');
$user->load = $this->request->getPost('load');
$user->save();
edited Oct '16

ya ..but i don't want to provide id in the form.....it has to be done automaticaly whenever i want to create a new user!!!



145.0k
Accepted
answer
edited Oct '16

Then just do this:

$user = new User();
$user->name = $this->request->getPost('name');
$user->load = $this->request->getPost('load');
$user->save();

Just dont set id at all :) It will be setted by mysql auto increment. Auto increment is happening in database. After saving it will update id from database using lastInsertId in model. So your model will have correct id from database.