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

Support Doctrine attributes alongside annotations in BaseTranslation #44

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions src/Entity/BaseTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @ORM\MappedSuperclass
*/
#[ORM\MappedSuperclass]
class BaseTranslation
{
/**
Expand All @@ -25,6 +26,9 @@ class BaseTranslation
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
protected $id;

/**
Expand All @@ -33,11 +37,13 @@ class BaseTranslation
* @PolyglotAnnotation\Locale
*/
#[Polyglot\Locale]
#[ORM\Column]
protected $locale;

/**
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false)
*/
#[ORM\JoinColumn(name: 'entity_id', referencedColumnName: 'id', nullable: false)]
protected $entity;

public function getLocale()
Expand Down
47 changes: 13 additions & 34 deletions tests/Functional/CascadePersistTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,24 @@ public function adding_and_persisting_translations(): void
}
}

/**
* @ORM\Entity
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
class CascadePersistTranslationsTest_Entity
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;

/**
* (!) There is *not* cascade="persist" configuration here.
*
* @ORM\OneToMany(targetEntity="CascadePersistTranslationsTest_Translation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \CascadePersistTranslationsTest_Translation::class, mappedBy: 'entity')]
protected Collection $translations;

/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
protected string|TranslatableInterface $text;

public function __construct()
Expand All @@ -85,33 +76,21 @@ public function addTranslation(string $locale, mixed $text): void
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class CascadePersistTranslationsTest_Translation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="CascadePersistTranslationsTest_Entity", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \CascadePersistTranslationsTest_Entity::class, inversedBy: 'translations')]
private CascadePersistTranslationsTest_Entity $entity;

/**
* @ORM\Column
*/
#[ORM\Column]
private string $text;
}
95 changes: 26 additions & 69 deletions tests/Functional/EntityInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,26 @@ public function testUpdateTranslations(): void
}
}

/**
* @ORM\Entity()
*
* @ORM\InheritanceType(value="SINGLE_TABLE")
*
* @ORM\DiscriminatorMap({"base"="EntityInheritance_BaseEntityClass", "child"="EntityInheritance_ChildEntityClass"})
*
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
#[ORM\InheritanceType(value: 'SINGLE_TABLE')]
#[ORM\DiscriminatorMap(['base' => 'EntityInheritance_BaseEntityClass', 'child' => 'EntityInheritance_ChildEntityClass'])]
#[ORM\DiscriminatorColumn(name: 'discriminator', type: 'string')]
class EntityInheritance_BaseEntityClass
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
private ?int $id = null;

private string $discriminator;

/**
* @ORM\OneToMany(targetEntity="EntityInheritance_BaseEntityClassTranslation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \EntityInheritance_BaseEntityClassTranslation::class, mappedBy: 'entity')]
private Collection $translations;

/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
private TranslatableInterface|string|null $text = null;

public function __construct()
Expand All @@ -157,52 +144,34 @@ public function getText(): TranslatableInterface
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_BaseEntityClassTranslation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="EntityInheritance_BaseEntityClass", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \EntityInheritance_BaseEntityClass::class, inversedBy: 'translations')]
private EntityInheritance_BaseEntityClass $entity;

/**
* @ORM\Column()
*/
#[ORM\Column]
private string $text;
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_ChildEntityClass extends EntityInheritance_BaseEntityClass
{
/**
* @ORM\Column(type="string")
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'string')]
private TranslatableInterface|string|null $extraText = null;

/**
* @ORM\OneToMany(targetEntity="EntityInheritance_ChildEntityClassTranslation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \EntityInheritance_ChildEntityClassTranslation::class, mappedBy: 'entity')]
private Collection $extraTranslations;

public function __construct()
Expand All @@ -222,33 +191,21 @@ public function getExtraText(): TranslatableInterface
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class EntityInheritance_ChildEntityClassTranslation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
private string $locale;

/**
* @ORM\ManyToOne(targetEntity="EntityInheritance_ChildEntityClass", inversedBy="extraTranslations")
*/
#[ORM\ManyToOne(targetEntity: \EntityInheritance_ChildEntityClass::class, inversedBy: 'extraTranslations')]
private EntityInheritance_ChildEntityClass $entity;

/**
* @ORM\Column()
*/
#[ORM\Column]
private string $extraText;
}
47 changes: 13 additions & 34 deletions tests/Functional/StronglyTypedTranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,24 @@ public function flush_updated_entity_two_times_does_not_update(): void
}
}

/**
* @ORM\Entity
*/
#[Polyglot\Locale(primary: 'en_GB')]
#[ORM\Entity]
class StronglyTypedTranslationsTest_Entity
{
/**
* @ORM\Column(type="integer")
*
* @ORM\Id
*
* @ORM\GeneratedValue
*/
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue]
public ?int $id = null;

/**
* @ORM\OneToMany(targetEntity="StronglyTypedTranslationsTest_Translation", mappedBy="entity")
*/
#[Polyglot\TranslationCollection]
#[ORM\OneToMany(targetEntity: \StronglyTypedTranslationsTest_Translation::class, mappedBy: 'entity')]
public Collection $translations;

/**
* @var TranslatableInterface<string>
*
* @ORM\Column(type="translatable_string", options={"use_text_column": true})
*/
#[Polyglot\Translatable]
#[ORM\Column(type: 'translatable_string', options: ['use_text_column' => true])]
public TranslatableInterface $text;

public function __construct()
Expand All @@ -224,33 +215,21 @@ public function __construct()
}
}

/**
* @ORM\Entity
*/
#[ORM\Entity]
class StronglyTypedTranslationsTest_Translation
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public ?int $id = null;

/**
* @ORM\Column
*/
#[Polyglot\Locale]
#[ORM\Column]
public string $locale;

/**
* @ORM\ManyToOne(targetEntity="StronglyTypedTranslationsTest_Entity", inversedBy="translations")
*/
#[ORM\ManyToOne(targetEntity: \StronglyTypedTranslationsTest_Entity::class, inversedBy: 'translations')]
public StronglyTypedTranslationsTest_Entity $entity;

/**
* @ORM\Column
*/
#[ORM\Column]
public string $text;
}
Loading