# How to insert records in DB table on plugin install

**URL:** <https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055>\
**Category:** International (English Only)\
**Tags:** programming\
**Created:** [12. April 2019 um 10:34 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055 "2019-04-12T10:34:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miro](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@miro](https://forum.shopware.com/u/miro)\
**Post date:** [12. April 2019 um 10:34 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055/1 "2019-04-12T10:34:37Z")

</div>

Hi to all,

since few days I am tring to create a payment&nbsp;plugin. I followed the developer documentation and at the moment my plugin&nbsp;have some simple functionalities. Unoftunally SW do not have any Refund statuses, so I want to add two custom records in&nbsp;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&nbsp;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!

---

<div class="post-metadata">

**Author:** ![Michael\_Telgmann](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/michael_telgmann/32/20289_2.png) [@Michael\_Telgmann](https://forum.shopware.com/u/Michael_Telgmann)\
**Post date:** [12. April 2019 um 10:42 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055/2 "2019-04-12T10:42:16Z")

</div>

Hi @miro‍

the crud\_service is probably not what you are looking for, because it is only for creating attributes&nbsp; ![Wink](https://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/wink.png "Wink")  
Have a look at this article&nbsp;[Shopware 5 - Tutorials & FAQs - Create new order & payment status](https://docs.shopware.com/en/shopware-5-en/tutorials-and-faq/create-new-order-and-payment-status)  
If you have further questions, just ask them here

Best regards from Schöppingen

![cool](http://forum.shopware.com/plugins/CKEditor/plugins/smiley/images/shopware.png "cool")&nbsp;Michael Telgmann

---

<div class="post-metadata">

**Author:** ![miro](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@miro](https://forum.shopware.com/u/miro)\
**Post date:** [12. April 2019 um 10:52 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055/3 "2019-04-12T10:52:10Z")

</div>

Thanks, I read this part, but asking the merchant to use phpmyadmin, or ask&nbsp;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&nbsp;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.

---

<div class="post-metadata">

**Author:** ![odessite](https://avatars.discourse-cdn.com/v4/letter/o/f05b48/32.png) [@odessite](https://forum.shopware.com/u/odessite)\
**Post date:** [12. April 2019 um 21:56 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055/4 "2019-04-12T21:56:42Z")

</div>

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_); }
```

---

<div class="post-metadata">

**Author:** ![miro](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@miro](https://forum.shopware.com/u/miro)\
**Post date:** [15. April 2019 um 06:47 UTC](https://forum.shopware.com/t/how-to-insert-records-in-db-table-on-plugin-install/59055/5 "2019-04-15T06:47:03Z")

</div>

Thanks!

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

Best regards!
