How to create a database table in Shopware 6?

Database table in Shopware 6

So, in this blog, we are gonna gain some knowledge on how to create a database table in Shopware6.
If you don't know what migrations are or how to create migrations, we've put up a guide on it, you can check it here.

php bin/console database:create-migration -p MageSparkHelloWorld --name createBlogTable

Kindly include our SQL procedures for creating database tables. Edit the freshly created file. In my case, the document is named

custom/plugins/MageSparkHelloWorld/src/Migration/Migration1649315274createBlogTable.php:

<?php declare(strict_types=1);

namespace MageSpark\HelloWorld\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1649315274createBlogTable extends MigrationStep
{
   public function getCreationTimestamp(): int
   {
       return 1649315274;
   }

   public function update(Connection $connection): void
   {
       $query = <<<SQL
           CREATE TABLE IF NOT EXISTS `magespark_blogs` (
               `id` BINARY(16) NOT NULL,
               `name` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
               `created_at` DATETIME(3) NOT NULL,
               `updated_at` DATETIME(3),
               PRIMARY KEY (`id`)
           ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL;
       $connection->executeStatement($query);

   }

   public function updateDestructive(Connection $connection): void
   {
       
   }
}

To initiate the migration, enter the following command:

php bin/console database:migrate MageSparkHelloWorld --all

 

Results Are:

Database table in Shopware 6

You’ll see the table that was created in the database! That was quick & simple! Right?

Database table in Shopware 6

Have a Wonderful Day!

 

Starting your own Shopware Store?

Get a free consultation from Shopware experts
on do's and don't

Talk to Experts
Talk to a Hyvä expert
Loading...