Magento 2 Data Patch Issue: Inserting Data into Custom Tables
As a senior developer working on a Magento 2 project, I recently encountered an issue while attempting to insert data into custom tables using the declarative schema approach. In this post, I’ll walk you through my step-by-step process of diagnosing and resolving the problem.
The Initial Setup
I created three custom tablesâchlorinators, equipment, and chlorinator_equipmentâusing the db_schema.xml file:
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<!-- The complete XML schema for the three tables is provided above -->
</schema>
Next, I wrote the necessary models and resource models to interact with these tables. For the chlorinators table, I created a model (Chlorinator.php) and a corresponding resource model (ResourceModel\Chlorinator.php).
The Data Patch Attempt
My goal was to insert data into the chlorinators table using a data patch. Here’s the AddData.php file where I attempted this:
class AddData implements \Magento\Framework\Setup\Patch\DataPatchInterface
{
// Constructor and dependencies
public function apply()
{
$chlorinatorData = [
'chlorinator_id' => 1,
'chlorinator_name' => 'chlorinator 1',
'has_pump' => 1
];
// Attempting to insert data using the model methods
$this->chlorinator->addData($chlorinatorData);
$this->chlorinator->getResource()->save($this->chlorinator);
}
// Other required methods for dependency and version management
}
The Error Encountered
When I attempted to create a new record, I encountered a Fatal error: Call to undefined method Jared\Submittal\Model\ResourceModel\Chlorator::create() error in the line where I called $this->chlorinator->addData($chlorinatorData).
The full error trace is provided above. The key issue was that I was trying to use a model function (addData) that is typically associated with the Magento\Framework\Model\AbstractModel class but was not defined in my custom resource model.
Resolving the Issue
Upon closer inspection, I realized the mistake lay in how I constructed my models. When extending from the abstract model and its resource model, I should have extended them directly instead of creating my own classes with the same structure.
To resolve this, I updated my Chlorinator.php file:
namespace Jared\Submittal\Model;
class Chlorinator extends \Magento\Framework\Model\AbstractModel
{
protected function _construct()
{
$this->_init('Jared\Submittal\Model\ResourceModel\Chlorinator');
}
}
And updated my ResourceModel\Chlorinator.php file:
namespace Jared\Submittal\Model\ResourceModel;
class Chlorinator extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init('chlorinators', 'chlorinator_id');
}
}
The Solution: Proper Model Inheritance
The key to solving this issue was ensuring that my models inherited the correct functionality from the abstract classes. By extending Magento\Framework\Model\AbstractModel for my model and using the same structure as defined in the framework, I gained access to all necessary CRUD operations.
Once I corrected the inheritance hierarchy, the error disappeared, and I was able to successfully insert data into my custom table using the following code:
public function apply()
{
$chlorinatorData = [
'chlorinator_id' => 1,
'chlorinator_name' => 'chlorinator 1',
'has_pump' => 1
];
// Correct way to insert data: use the model's save() method
$this->chlorinator->setData($chlorinatorData);
$this->chlorinator->save();
}
Conclusion
When working with custom tables in Magento 2, it is crucial to follow the proper model inheritance structure. Extending from the framework-provided abstract models ensures access to all necessary CRUD operations.
Remembering this best practice will help you avoid similar errors in the future while developing Magento extensions or modules.
For more insights and best practices on Magento development, feel free to explore our resources at Laravel Company.