Search code examples
mysqlormfat-free-framework

How to use fat-free ORM to insert data to id only table?


I have this table

CREATE TABLE IF NOT EXISTS `group` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;

This is work.

INSERT INTO `group` (`id`) VALUES (NULL);

But I cannot insert record using fat-free

I tried this.

$group=new DB\SQL\Mapper($f3->get('DB'),'group');
$group->save();

and this.

$group=new DB\SQL\Mapper($f3->get('DB'),'group');
$group->id=null;
$group->save();

and this.

$group=new DB\SQL\Mapper($f3->get('DB'),'group');
$group->id='null';
$group->save();

But no one can create record, and also no error.

What is correct way to insert record to this table?


Solution

  • Fat-Free Framework's Mapper doesn't insert records without (changed/new) values besides the primary key. So the $fields array is empty and the Mapper skips the insert operation.

    I am not sure if this qualifies as bug.

    Here is a simple solution based on your provided query and F3's SQL object:

    $sql = $f3->get('DB');
    $sql->exec('INSERT INTO group (id) VALUES (NULL)');