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');
}
  • Please check your IntallSchema script, field id primary key must have 'identity' => true or 'auto_increment' => true for save autoincrement value – Prince Patel Dec 26 '17 at 7:11
  • Can you share the constructor of your resourcemodel? – Purushotam Sangroula Dec 26 '17 at 7:17
  • Yes, it has 'auto_inctement'=> true. And it table i see correct ids for new entities. – Styopchik Dec 26 '17 at 7:18
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, privacy policy and cookie policy

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