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

Duplication in create/save() when app is stopping

I create some data from model :

<?php
$record = new Records();
// ...
$record->create()

Now, when I stop my app (or I have some errors) - for example:

<?php
print_r($record->id);
exit();

App does not add a single record, but two. Why this is work like that?



98.9k

Can you please provide a script like this: https://gist.github.com/phalcon/5610396 making us able to reproduce your problem?

Well, there is no problem for me, because my app works fine. I noticed it by chance and I ask out of curiosity - it is a bug or not?

There is very standard configuration, so I paste only controller and model. (Phalcon V 1.1/nginx/php5.4)

<?php namespace Front\Controllers;

class TestController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

        $record = new \Models\Records();
        $record->module = 1;
        $record->create();
        $id = $record->id;
        print_r($id);
        exit();

    }
}
<?php namespace Models;

class Records extends \Phalcon\Mvc\Model
{

    public $id;
    public $module;

    public function getSource()
    {
        return 'records';
    }

}

CREATE TABLE IF NOT EXISTS records (id int(10) unsigned NOT NULL AUTO_INCREMENT, module tinyint(3) unsigned NOT NULL, PRIMARY KEY (id), KEY module (module)) ENGINE=InnoDB AUTO_INCREMENT=1 ;



98.9k

This is clearly not a bug, but for the sake of clarity, this is your code: https://gist.github.com/phalcon/5886245 and it's running here: https://test.phalcon.io/create.php as you can see just a single insert is made. And the "End example (never executed)<br>" is not being printed because of the call to 'exit'.

In addition, there are unit-tests for that like these: https://github.com/phalcon/cphalcon/blob/master/unit-tests/ModelsTest.php#L302, running and passing on 3 different versions of PHP (https://www.travis-ci.org/phalcon/cphalcon) (without mention the active applications and functionality that depends on that).