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

Create new record without data

Hi,

how do I mange to insert an empty record? I always get "Unable to insert into ..... without data"exception.

<?php

use Phalcon\Mvc\Model;

/**
 * Settings.
 *
 * @Source("test")
 */
class TestModel extends Model
{

    /**
     * @Primary
     * @Identity
     * @Column(type="integer", nullable=false, size="11")
     */
    public $id;

}
<?php

$test = new TestModel;
$test->save();


98.9k

This happens because the only column the mapped table has is auto_numeric thus it's ignored and there are no more columns to insert making `Phalcon\Db\Adapter->insert throw an exception. Adding a non-auto numeric column to your model will solve the problem.

Thanks for the reply. Ok, so I have to add a "dummy" column to the model. Any chance to make it possible in future versions?



3.2k
Accepted
answer

Hi, just like to share my solution.

I extended the Mysql Pdo adapter to change the "useExplicitIdValue" setting and now it is possible to insert into tables without data fields.

class Mysql extends \Phalcon\Db\Adapter\Pdo\Mysql
{
    public function useExplicitIdValue()
    {
        return true;
    }
}

What do you think about making this setting accessible through constructor options ?

I use 2.0.x and the solution still works.

Try to extend your adapter. The function is located in adapter classes. Look at my post from Aug'14.