Model after save id becomes 0 (phalcon 3.1.2-2)

Hello,

i get id zero after use model->save()

example :

    $photos = new Photos();
    $photos->name = $sGenerateName;
    $photos->size = $file->getSize();
    $photos->save() ;
   var_dump($photos->id) ;   // i get string '0' (length=1)


2.9k
edited Jun '17

Hello

Have you check in your database if the record is created

you can use

if (!$photos->save())
{
    echo "error message";
}

edit : also can you post your database schema

edited Jun '17

Hello,

  1. Check your DB and see if the id is AUTO_INCREMENT;
  2. If (1) didnt sovle the problem, use $photos->getMessages() to see errors of the save operation. Maybe you have not null column in DB? Like phil67000 suggested.
  3. Maybe you are using Oracle, where there is no AI?

It's created fine on the database and the field AUTO_INCREMENT and fine in the table, but i return 0 when i print id after save() method, the $photos->getMessages() return null (no error), i am using php7 with mysql on ubuntu 16.04

Try calling $Photos->refresh() before asking for the id. save() is supposed to retrieve the id using LASTINSERTID, but something isn't working here. In all likelihood it's something in your setup but I don't know what that would be.

Dylan's suggestion made me think of something... you can try using the $photos->create() instead of ->save() method.