I have created a new table and added a few data using Data patch
setup/patch/data/.
Now I want to add some more data and want to update few data's so can anyone please help me or guide me that how can I do that?
Thanks
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityInject Magento\Framework\Setup\ModuleDataSetupInterface
on your data patch class:
Add below code add for adding to that table
$this->moduleDataSetup->startSetup();
$setup = $this->moduleDataSetup;
$data[] = ['field1' => 'Value11', 'field2' => 'Value12'];
$data[] = ['field1' => 'Value21', 'field2' => 'Value22'];
$this->moduleDataSetup->getConnection()->insertArray(
$this->moduleDataSetup->getTable('your_table'),
['field1', 'field2'],
$data
);
$this->moduleDataSetup->endSetup();
Devbera\TestPracticsPlugin\Setup\Patch\Data\TestInfo
.A nd Changes it according to your module and patch class.<?php
namespace Devbera\TestPracticsPlugin\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class TestInfo implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
public function __construct(
ModuleDataSetupInterface $moduleDataSetup
) {
$this->moduleDataSetup = $moduleDataSetup;
}
public function apply()
{
$this->moduleDataSetup->startSetup();
$setup = $this->moduleDataSetup;
$data[] = ['field1' => 'Value11', 'field2' => 'Value12'];
$data[] = ['field1' => 'Value21', 'field2' => 'Value22'];
$this->moduleDataSetup->getConnection()->insertArray(
$this->moduleDataSetup->getTable('your_table'),
['field1', 'field2'],
$data
);
$this->moduleDataSetup->endSetup();
}
public function getAliases()
{
return [];
}
public static function getDependencies()
{
return [];
}
}
TestInfo
in this case above) from the patch_list table in Magento2 DB, then run php bin/magento setup:upgrade
again. Need to be careful though...
The patch will only be applied once and that is when you run bin/magento setup:upgrade
When the patch is successfully executed, Magento will insert a record into the patch_list table in the database, so in order to add some more data and edit few data, you have to remove the record from the patch_list table, which will allow the patch to be executed again when running bin/magento setup:upgrade
hope the answer will help future developers
the source this is a great article where it's explained in detail.
You can "add some more data" as @Amit Bera suggested you and then you can "update few data" changing $this->moduleDataSetup->getConnection()->insertArray($table, array $columns, array $data);
with $this->moduleDataSetup->getConnection()->update($table, array $bind, $where = '');
See this class: vendor/magento/framework/DB/Adapter/AdapterInterface.php
I think you need to create one more patch file for add some more data and update data's and your second file name like "FirstFileNameV2".
Then run bin/magento setup:upgrade command.
<?php
declare(strict_types=1);
namespace Moazzam\AddPatch\Setup\Patch\Data;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;
use Moazzam\AddPatch\Model\PetFactory;
use Moazzam\AddPatch\Model\ResourceModel\Pet as ResourceModelPet;
/**
* Add data in manage_pet table
*
* Class AddData
*/
class AddData implements DataPatchInterface, PatchVersionInterface
{
/**
* @var ModuleDataSetupInterface
*/
private ModuleDataSetupInterface $moduleDataSetup;
/**
* @var PetFactory
*/
private PetFactory $petFactory;
/**
* @var resourceModelPet
*/
private resourceModelPet $resourceModelPet;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param resourceModelPet $resourceModelPet
* @param PetFactory $petFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
ResourceModelPet $resourceModelPet,
PetFactory $petFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->petFactory=$petFactory;
$this->resourceModelPet=$resourceModelPet;
}
/**
* Apply method is used for apply the data patch
*
* @inheritdoc
*/
public function apply()
{
$sampleData = [
[
'pet_name' => 'German Shepard',
'customer_id' => '1'
],
[
'pet_name' => 'German Dog',
'customer_id' => '2'
]
];
foreach ($sampleData as $data) {
$insertData = $this->petFactory->create()->setData($data);
$this->resourceModelPet->save($insertData);
}
}
/**
* @inheritdoc
*/
public static function getDependencies(): array
{
return [];
}
/**
* @inheritdoc
*/
public function getAliases(): array
{
return [];
}
/**
* @return string
*/
public static function getVersion(): string
{
return " ";
}
}
Each patch can only be executed once when you run bin/magento setup:upgrade. When the patch is successfully executed, Magento will insert a record into the patch_list table in the database with the patch_name is the full name path of our patch class.
There are two ways to modify data applied by data patch.
First, clear all data in the table you want to modify.
Then you can either:
Or