Magento – Magento 2 : I Can get Collection from database but cannot save data in database

databasemagento2module

I have a custom table in which I want to save data.
I tried below different methods to save data in table.But cannot get success.
Please check below methods :

Method 1 :

$model = $this->_objectManager->create('Namesapce\Modulename\Model\Test\Product');
$model->setRowId(1);
$model->setProductId(2048);
$model->save();

Method 2 :

$model = $this->_objectManager->create('Namesapce\Modulename\Model\Test\Product');
$data = ['row_id' => 1, 'product_id' => '2048'] ;
$model->setData($data);
$model->save();

For testing purpose I have manually insert data in table and then try to getcollection.

Strange thing is I can sucessfully get collection of data from table.but can not save. I also put above code in try catch but it always going in try. Not in catch.

Please take me out from this issue.
Thank you.

Best Answer

You need to be using a resource model ModelFactory, rather than trying to create through the object manager.

Related Topic