From 79e7ec0a19932456296ecfb93ea381f25b38fcb1 Mon Sep 17 00:00:00 2001 From: lukasdrahy Date: Tue, 25 Jul 2023 16:32:24 +0200 Subject: [PATCH] Implement knplabs/doctrine-behaviors - todo \Knp\DoctrineBehaviors\EventSubscriber\TranslatableEventSubscriber::loadClassMetadata() method is not called, subsriber is - same issue described here https://github.com/KnpLabs/DoctrineBehaviors/issues/729 --- src/Bundles/AP/Entity/Message.php | 58 ++++++------------- src/Bundles/AP/Entity/MessageTranslation.php | 41 +++++++++++++ ...y.php => MessageTranslationRepository.php} | 6 +- 3 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 src/Bundles/AP/Entity/MessageTranslation.php rename src/Bundles/AP/Repository/{MessageRepository.php => MessageTranslationRepository.php} (59%) diff --git a/src/Bundles/AP/Entity/Message.php b/src/Bundles/AP/Entity/Message.php index 1b5097c..073aeb4 100644 --- a/src/Bundles/AP/Entity/Message.php +++ b/src/Bundles/AP/Entity/Message.php @@ -4,67 +4,45 @@ namespace App\Bundles\AP\Entity; -use App\Repository\ProductRepository; use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\Mapping\Entity; +use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait; +use Symfony\Contracts\Translation\TranslatableInterface; +use Symfony\Contracts\Translation\TranslatorInterface; -// #[Entity(repositoryClass: ProductRepository::class)] -#[Entity] +#[ORM\Entity] #[ORM\Table(name: 'ap_message')] -#[ORM\UniqueConstraint(name: 'unique_idx', columns: ['key', 'language'])] -class Message +class Message implements TranslatableInterface { + use TranslatableTrait; + + public const URGENCY_INFO = 'info'; + public const URGENCY_CRITICAL = 'critical'; + #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; - #[ORM\Column(length: 255)] - private ?string $key; - - /** - * @see https://symfony.com/doc/5.4/components/intl.html - * - * @var string|null $language Language code - */ - #[ORM\Column(length: 255)] - private ?string $language; - - #[ORM\Column] - private ?string $text = null; + #[ORM\Column(length: 32)] + private string $urgency; public function getId(): ?int { return $this->id; } - public function getKey(): ?string - { - return $this->key; - } - - public function setKey(?string $key): void - { - $this->key = $key; - } - - public function getLanguage(): ?string - { - return $this->language; - } - - public function setLanguage(?string $language): void + public function getUrgency(): string { - $this->language = $language; + return $this->urgency; } - public function getText(): ?string + public function setUrgency(string $urgency): void { - return $this->text; + $this->urgency = $urgency; } - public function setText(?string $text): void + public function trans(TranslatorInterface $translator, string $locale = null): string { - $this->text = $text; + // TODO: Implement trans() method. } } diff --git a/src/Bundles/AP/Entity/MessageTranslation.php b/src/Bundles/AP/Entity/MessageTranslation.php new file mode 100644 index 0000000..a07d067 --- /dev/null +++ b/src/Bundles/AP/Entity/MessageTranslation.php @@ -0,0 +1,41 @@ +id; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): void + { + $this->text = $text; + } +} diff --git a/src/Bundles/AP/Repository/MessageRepository.php b/src/Bundles/AP/Repository/MessageTranslationRepository.php similarity index 59% rename from src/Bundles/AP/Repository/MessageRepository.php rename to src/Bundles/AP/Repository/MessageTranslationRepository.php index c9c00cf..d27a638 100644 --- a/src/Bundles/AP/Repository/MessageRepository.php +++ b/src/Bundles/AP/Repository/MessageTranslationRepository.php @@ -4,14 +4,14 @@ namespace App\Bundles\AP\Repository; -use App\Bundles\AP\Entity\Message; +use App\Bundles\AP\Entity\MessageTranslation; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; -class MessageRepository extends ServiceEntityRepository +class MessageTranslationRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { - parent::__construct($registry, Message::class); + parent::__construct($registry, MessageTranslation::class); } }