Skip to content

Commit

Permalink
Implement knplabs/doctrine-behaviors - todo
Browse files Browse the repository at this point in the history
\Knp\DoctrineBehaviors\EventSubscriber\TranslatableEventSubscriber::loadClassMetadata() method is not called, subsriber is - same issue described here KnpLabs/DoctrineBehaviors#729
  • Loading branch information
hluchas committed Jul 25, 2023
1 parent 25bd6c7 commit 79e7ec0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
58 changes: 18 additions & 40 deletions src/Bundles/AP/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
41 changes: 41 additions & 0 deletions src/Bundles/AP/Entity/MessageTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Bundles\AP\Entity;

use App\Bundles\AP\Repository\MessageTranslationRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;

#[Entity(repositoryClass: MessageTranslationRepository::class)]
#[ORM\Table(name: 'ap_message_translation')]
class MessageTranslation implements TranslationInterface
{
use TranslationTrait;

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\Column]
private ?string $text = null;

public function getId(): ?int
{
return $this->id;
}

public function getText(): ?string
{
return $this->text;
}

public function setText(?string $text): void
{
$this->text = $text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 79e7ec0

Please sign in to comment.