Entity Creation
Model creation is fully featured in our DevTools plugin - simply run econsor:dev:addmodel
Shopware's own docs are accurate although they don't detail how you can simplify the creation of both Migration and Entity/Collection classes.
Simplify creation of own Database Models
Step 1:
Create your EntityDefinition Class in the appropriate folder and register it in the services.xml like this:
<service id="Swag\CustomEntity\Custom\CustomEntityDefinition">
<tag name="shopware.entity.definition" entity="custom_entity" />
</service>
Step 2:
Install and activate your plugin.
Step 3:
Via SSH, run the following commands:
bin/console dal:create:entities
bin/console dal:create:schema
Step 4:
This will have generated a folder called "schema" in your Shopware root folder. Check this folder for 2 things: First, a subfolder has been created with the full or partial name of your database entity (the one you have assigned in the EntityDefinition). In there you can find the generated EntityCollection and EntityEntity files. Copy them in the folder where your entity definition is and rename the files and namespaces accordingly. They will not be correct. Secondly, an .sql file has been created with the full or partial name of your database entity. In there you will be able to find the complete SQL Query for your Migration file.
Creating a Migration file
For your Database Table to be installed in your DB, you must create a migration file. For this, simply run:
bin/console database:create-migration --plugin **PLUGINNAME** --name **MIGRATIONNAME**
This will create a migration file. In there you will be able to run SQL Commands to install/update/modify Database tables. Use the SQL query you generated and run it in the update() Method of your Migration.
Uninstall Context
In your Plugin base class, you should remove the Tables when you uninstall the Plugin. This is defined in the Shopware Plugin Guidelines.
To do this, add this to your uninstall() Method:
if($context->keepUserData()) {
return;
}
$this->removeMigrations();
$connection = $this->container->get(Connection::class);
$connection->executeQuery('DROP TABLE IF EXISTS `your_table_name`');
Important: Always delete translation tables before the normal ones!
The if ($context->keepUserData()) will make it so this will be skipped when the user made the respective settings to keep Plugin Data.
Entity Translation
Refer to the Shopware Docs for this.
Add Entities via API
When you add Entities via Repositories in SW6, it will trigger all associated Processes like Indexing etc. This will heavily Impact Shop Performance. To avoid this, we can use Shopwares Sync API
Step 1
Create a Command and register it with this argument in the services.cml:
<argument type="service" id="Shopware\Core\Framework\Api\Sync\SyncService"/>
In the Command, import these Components:
use Shopware\Core\Framework\Api\Sync\SyncService;
use Shopware\Core\Framework\Api\Sync\SyncBehavior;
use Shopware\Core\Framework\Api\Sync\SyncOperation;
Step 2
Initialise the SyncService and -Behavior like this:
$this->syncService = new SyncService()
$this->syncBehavior = new SyncBehavior(true, true, 'disable-indexing');
SyncBehaviour has three parameters - (failOnError, singleOperation, syncBehaviour).
failOnError boolean
singleOperation boolean - true means, that all Data is written at once
syncBehaviour string / null - has 3 Options:
- Option 1: null All associated Processes like Indexing are being triggered.
- Option 2: use-queue-indexing Indexing is being done in the Background.
- Option 3: disable-indexing No Indexing is being done.
Then, you add (an) operation(s):
$operations[] = new SyncOperation('pr' . $productNumber, 'product', 'upsert', $swProduct, 3);
- Parameter 1:
key, that is being returned in the Result - Parameter 2:
(string)which entity - Parameter 3:
(string)which Operation (upsert or delete) - Parameter 4:
Payload Array - Parameter 5:
(int)API Version
Step 3
Now we can use the SyncService like this:
$result = $this->syncService->sync($operations, $this->context, $this->syncBehavior);