Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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();


94.5k

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?



2.5k
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.