Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(translatable): Add a tips about Doctrine Migration #716

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/translatable-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Translatable - Keep existing content with Doctrine Migration

When adding Translatable to an existing project, Doctrine Migration is going to blindly remove your table columns as you moved them to Translatable dedicated entities.

Here is how you can edit your Doctrine Migration to keep your contributed database content.

Let's see an example with a **Product** entity owning a **description** property. You moved this property to the **ProductTranslation** entity. Your initial Doctrine migration will look like this:

```php
final class Version20221111111111 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE product_translation (id INT AUTO_INCREMENT NOT NULL, translatable_id INT DEFAULT NULL, description LONGTEXT DEFAULT NULL, locale VARCHAR(5) NOT NULL, INDEX IDX_BE61F5EA2C2AC5D3 (translatable_id), UNIQUE INDEX product_translation_unique_translation (translatable_id, locale), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE product_translation ADD CONSTRAINT FK_BE61F5EA2C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES product (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE product DROP description');
}

// ...
}
```

The last SQL is problematic: `DROP description` will permanently delete all the contributed descriptions.

All you need to do is add a new SQL statement manually, between the **CREATE TABLE** and the **DROP description**, to move your content using the **INSERT ... SELECT** notation:

```sql
INSERT INTO product_translation (translatable_id, description, locale) SELECT id, description, 'en' FROM product;
```

The modification is as follows:

```diff
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE product_translation (id INT AUTO_INCREMENT NOT NULL, translatable_id INT DEFAULT NULL, description LONGTEXT DEFAULT NULL, locale VARCHAR(5) NOT NULL, INDEX IDX_BE61F5EA2C2AC5D3 (translatable_id), UNIQUE INDEX product_translation_unique_translation (translatable_id, locale), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE product_translation ADD CONSTRAINT FK_BE61F5EA2C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES product (id) ON DELETE CASCADE');
+ $this->addSql('INSERT INTO product_translation (translatable_id, description, locale) SELECT id, description, 'en' FROM product;');
$this->addSql('ALTER TABLE product DROP description');
}
```

Make sure to add all your fields and edit the locale if you don't come from "en" like in the example.
7 changes: 5 additions & 2 deletions docs/translatable.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ same folder, e.g.:

The default naming convention (or its customization via trait methods) avoids you to manually handle entity associations. It is handled automatically by the `TranslatableEventSubscriber`.

[How to use translatable with api platform?](/docs/translatable-api-platform.md)
Related documentations:

- [How to use translatable with api platform?](/docs/translatable-api-platform.md)
- [How to tweak your Doctrine Migration to keep existing content?](/docs/translatable-migration.md)

## Entity

Expand Down Expand Up @@ -228,4 +231,4 @@ No joins are created. Only the query strategy is changed.
|------------|------------------------------------------------------------------------------------------------------------------------------------|
| EAGER | Doctrine always loads the translation. |
| LAZY | Doctrine only loads the translation if it's actually used. |
| EXTRA_LAZY | Doctrine only loads the translation if the data is specifically queried. Count and/or contains queries do not trigger new queries. |
| EXTRA_LAZY | Doctrine only loads the translation if the data is specifically queried. Count and/or contains queries do not trigger new queries. |