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

Model UPDATE on save() when must be INSERT

There is big problem with models in Phalcon 1.1. I have similar code like this:

class User extends \Phalcon\Mvc\Model
{
    public $id;    
    public $email;
    function myCustomUserCreator(){
        $newUser = new User();
        $newUser->email= 'test'; 
            if($newUser->save()==false){                
                return false;
            } else {                 
                return $newUser->id;
            }
    }
}
print User::myCustomUserCreator();

This code always execute UPDATE and on execution returns new values from mysql auto increment (1,2,3,4 etc.) This is very strange, because I wrote "new User()", what means that I have to INSERT new record in table with save(). Can somebody explain why it works so or it is some phalcon bug? In other frameworks (Yii, Zend) this works without problems.



98.9k

This is your code: https://gist.github.com/phalcon/5610308

I'm running it here: https://test.phalcon.io/orm-1.php

As you can see, there are only INSERTs returning the numeric id generated by the database



16.1k

try to change Phalcon\Db\Adapter\Pdo\Sqlite as Connection, to Phalcon\Db\Adapter\Pdo\Mysql as Connection

I tried this code locally and it also try to update. If I write myCustomUserCreator function code outside User class, for example in controller action method then everything is fine.



98.9k

Code: https://gist.github.com/phalcon/5610396

Running: https://test.phalcon.io/orm-2.php

SELECT IF(COUNT()>0, 1 , 0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME= 'user' AND TABLE_SCHEMA='test' SELECT IF(COUNT()>0, 1 , 0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='user' DESCRIBE user INSERT INTO user (email) VALUES (?) 5