Person.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Translator\Fixture;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Translator\ObjectTranslator;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. /**
  7. * @ORM\Entity
  8. * @ORM\HasLifecycleCallbacks
  9. */
  10. class Person
  11. {
  12. /**
  13. * @ORM\Id
  14. * @ORM\GeneratedValue
  15. * @ORM\Column(type="integer")
  16. */
  17. private $id;
  18. /**
  19. * @ORM\Column(name="name", type="string", length=128)
  20. */
  21. private $name;
  22. /**
  23. * @ORM\Column(name="desc", type="string", length=128)
  24. */
  25. private $description;
  26. public function getId()
  27. {
  28. return $this->id;
  29. }
  30. public function setName($name)
  31. {
  32. $this->name = $name;
  33. }
  34. public function getName()
  35. {
  36. return $this->name;
  37. }
  38. public function setDescription($description)
  39. {
  40. $this->description = $description;
  41. }
  42. public function getDescription()
  43. {
  44. return $this->description;
  45. }
  46. //
  47. // TRANSLATIONS DEFINITION:
  48. //
  49. /**
  50. * @ORM\OneToMany(targetEntity="PersonTranslation", mappedBy="translatable", cascade={"persist"})
  51. */
  52. private $translations;
  53. private $translator;
  54. public function __construct()
  55. {
  56. $this->translations = new ArrayCollection();
  57. $this->initializeTranslator();
  58. }
  59. /** @ORM\PrePersist */
  60. public function translateEntityToDefaultLocale()
  61. {
  62. $this->translator->translate();
  63. }
  64. /** @ORM\PostLoad */
  65. public function initializeTranslator()
  66. {
  67. if (null === $this->translator) {
  68. $this->translator = new ObjectTranslator($this,
  69. /* List of translatable properties: */ array('name'),
  70. /* Translation entity class: */ 'Translator\Fixture\PersonTranslation',
  71. /* Translations collection property: */ $this->translations
  72. );
  73. return;
  74. }
  75. $this->translateEntityToDefaultLocale();
  76. }
  77. public function translate($locale = null)
  78. {
  79. return $this->translator->translate($locale);
  80. }
  81. }