XF 2.0 Entity Manager Create Method

Myke623

Well-known member
In order to insert a new record (entity), the Entity Manager (em) create method is being used:

PHP:
$note = $this->em()->create('Demo\Scratchpad:Note');

In my 'note' entity, the 'note_id' is an auto incrementing primary key, however, when I invoke the formAction with a basicEntitySave($note, $input), I get the following error:

Oops! We ran into some problems.
Please enter a value for the required field 'note_id'.

What am I not doing correctly? Surely I don't need to determine the next available note_id, right?

My Note entity definition is as follows:
PHP:
class Note extends \XF\Mvc\Entity\Entity
{
    public static function getStructure(Structure $structure)
    {
        $structure->table = 'xf_demo_scratchpad';
        $structure->shortName = 'Demo\Scratchpad:Note';
        $structure->primaryKey = 'note_id';
        $structure->columns = [
            'note_id' => ['type' => self::UINT, 'required' => true, 'autoIncrement' => true],
            'user_id' => ['type' => self::UINT, 'required' => true],
            'note_date' => ['type' => self::UINT, 'default' => time()],
            'message' => ['type' => self::STR, 'required' => true],
            'note_order' => ['type' => self::UINT]
        ];
        $structure->getters = [];
        $structure->relations = [
            'User' => [
                'entity' => 'XF:User',
                'type' => self::TO_ONE,
                'conditions' => 'user_id',
                'primary' => true
            ],
        ];
        
        return $structure;
    }
}
 
Replace “required” with “nullable” for the note_id line.

The entity will be blocked from saving because it’s expecting to have a note_id value when in reality it should be null which will trigger MySQL to automatically set an auto incrementing value on save.
 
Back
Top Bottom