0

I have a table with only 2 fields: id (primary key) and name. I've created a model for it, repository model.

In my repository model, there is a method save. I'm trying to create a new record in my table, executing:

$model = `$this->modelFactory->create();
$model->setName('some name');
$this->modelRepository->save($model);

Model saves succefully, but, when i try to get an id of created model, it returns null, so i have only name inside of model ($model->getName() => 'some name', $model->getId() => null)

So, how do I get new record id?

P.S. New record appears in DB with correct id.

There is a constructor of resource model:

/**
 * Initialize resource model and define main table
 *
 * @return void
 */
protected function _construct()
{
    $this->_init('catalog_motorbike_vendors', 'id');
}
3
  • Please check your IntallSchema script, field id primary key must have 'identity' => true or 'auto_increment' => true for save autoincrement value Commented Dec 26, 2017 at 7:11
  • Can you share the constructor of your resourcemodel?
    – P S
    Commented Dec 26, 2017 at 7:17
  • Yes, it has 'auto_inctement'=> true. And it table i see correct ids for new entities.
    – Styopchik
    Commented Dec 26, 2017 at 7:18

1 Answer 1

0

Found a mistake in my own code. There was not implemented method setId() in my Model class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.