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


82.7k

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?



1.4k
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 ?



43.0k

This doesn't seem to work in Phalcon 2.0.0.

I use 2.0.x and the solution still works.



43.0k

It doesn't seem to ever call useExplicitIdValue().



43.0k

Yes. It wasn't calling that function in my custom dialect class.

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



43.0k

Thanks, that makes sense. My head must have been stuck in the other two problems I was trying to solve. I added a filler field to get around it until I could find the solution.