1

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.

1
  • we can get more close to your issue if we could see full code of your save controller & Model Collection May 4 '16 at 12:54
1

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

0

Please Flush magento cache and try to save again if it's not work, then See your log folder and do some debugging in your module:

1
  • I tried to flush magento cache and also check log folder.But no error is found.
    – Krupali
    Mar 28 '16 at 10:44
0
$model = $this->_objectManager->create('Namesapce\Modulename\Model\Test\Product')->getCollection();
$model->setRowId(1);
$model->setProductId(2048);
$model->save();
1
  • 1
    Please elaborate a bit more on what your code example is doing and how it will solve the OP's problem.
    – 7ochem
    May 4 '16 at 10:26
-1

I had faced same problem when working on Magento2 CRUD. Never use Primary Key with model save() method.

Definately row_id will be primary key.

How to check we created the correct model , resource & collection class in magento.

In magento2 $this->_objectManager interface use for creating the model object.

$subscription = $this->_objectManager->create('path-to-model class');

Example: Abc\Hello\Model\Subscription.

Use get_class($subscription) method. This will show the full class path.

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.