How to insert records in DB table on plugin install

Hi to all,

since few days I am tring to create a payment plugin. I followed the developer documentation and at the moment my plugin have some simple functionalities. Unoftunally SW do not have any Refund statuses, so I want to add two custom records in s_core_states. Of cource to be harder I want to do this during plugin install, and for the moment I can not find how to do it.

During the install I use crud_service but as I understand it can edit the schema, but can not do insert.

I will be glad is someone help or give me advice how to do it!

Best regards!

Hi @miro‍

the crud_service is probably not what you are looking for, because it is only for creating attributes  Wink
Have a look at this article Shopware 5 - Tutorials & FAQs - Create new order & payment status
If you have further questions, just ask them here

Best regards from Schöppingen

cool Michael Telgmann

Thanks, I read this part, but asking the merchant to use phpmyadmin, or ask him to ask the hosting provider to do this is not an option.

I think this must bu done from the plugin. I can insert this records from some other method, and will include a check in it, but this will be fired everytime when the method is called. The only method we call once is the install method.

As alternative I serached for hook - on-plugin-config-save or somehing like this, but for the moment is is hard to undestand or find the event I need.

You can insert new states while install or activate methods:

_/\*\*_ _\* This method can be overridden_ _\*_ _\*_ _@param_ _InstallContext $context_ _\*/_ **public function** install(InstallContext $context) { **parent** ::_install_($context); **if** (!$this-\> **container** -\>get( **'db'** )-\>fetchOne( **"SELECT `id` FROM `s_core_states` WHERE `name`=?"** , [$name])) { $this-\> **container** -\>get( **'db'** )-\>insert( **'s\_core\_states'** , [**'name'** =\> $name, **'description'** =\> $description, **'position'** =\> $position, **'group'** =\> $group, **'mail'** =\> $sendMail,] ); } } _/\*\*_ _\* This method can be overridden_ _\*_ _\*_ _@param_ _ActivateContext $context_ _\*/_ **public function** activate(ActivateContext $context) { **if** (!$this-\> **container** -\>get( **'db'** )-\>fetchOne( **"SELECT `id` FROM `s_core_states` WHERE `name`=?"** , [$name])) { $this-\> **container** -\>get( **'db'** )-\>insert( **'s\_core\_states'** , [**'name'** =\> $name, **'description'** =\> $description, **'position'** =\> $position, **'group'** =\> $group, **'mail'** =\> $sendMail,] ); } $context-\>scheduleClearCache(InstallContext::_CACHE\_LIST\_ALL_); }
2 „Gefällt mir“

Thanks!

It works great, I still do not know what are all methods (services) I can get with $this-> container ->get(), I have to check this out.

Best regards!