public function testTranslatableWithMagicProperties() { $person = new Person(); $person->translate('en')->setName('Jen'); $person->translate('ru_RU')->name = 'Женя'; $person->translate('ru_RU')->description = 'multilingual description'; $this->assertSame('Jen', $person->name); $this->assertSame('Jen', $person->translate()->name); $this->assertSame('Женя', $person->translate('ru_RU')->name); $this->assertSame('multilingual description', $person->translate('ru_RU')->description); $this->assertSame('multilingual description', $person->description); }
public function testTranslatable() { $person = new Person(); $person->translate()->setName('Jen'); $person->translate('ru_RU')->setName('Женя'); $person->setDescription('description'); $person->translate('ru_RU')->setDescription('multilingual description'); $this->assertSame('Jen', $person->translate()->getName()); $this->assertSame('Женя', $person->translate('ru_RU')->getName()); $this->assertSame('multilingual description', $person->getDescription()); $this->assertSame('multilingual description', $person->translate()->getDescription()); $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription()); $this->em->persist($person); $this->em->flush(); $this->em->clear(); // retrieve record (translations would be fetched later - by demand) $person = $this->em->getRepository(self::PERSON)->findOneByName('Jen'); $this->assertSame('Jen', $person->getName()); $this->assertSame('Jen', $person->translate()->getName()); $this->assertSame('Женя', $person->translate('ru_RU')->getName()); $this->assertSame('multilingual description', $person->getDescription()); $this->assertSame('multilingual description', $person->translate()->getDescription()); $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription()); // retrieve record with all translations in one query $persons = $this->em->getRepository(self::PERSON)->createQueryBuilder('p')->select('p, t')->join('p.translations', 't')->getQuery()->execute(); $person = $persons[0]; $this->assertSame('Jen', $person->getName()); $this->assertSame('Jen', $person->translate()->getName()); $this->assertSame('Женя', $person->translate('ru_RU')->getName()); $this->assertSame('multilingual description', $person->getDescription()); $this->assertSame('multilingual description', $person->translate()->getDescription()); $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription()); $person->translate('es_ES')->setName('Amigo'); $this->em->flush(); // retrieve record with all translations in one query $persons = $this->em->getRepository(self::PERSON)->createQueryBuilder('p')->select('p, t')->join('p.translations', 't')->getQuery()->execute(); $person = $persons[0]; $this->assertSame('Jen', $person->getName()); $this->assertSame('Jen', $person->translate()->getName()); $this->assertSame('Женя', $person->translate('ru_RU')->getName()); $this->assertSame('Amigo', $person->translate('es_ES')->getName()); $this->assertSame('multilingual description', $person->getDescription()); $this->assertSame('multilingual description', $person->translate()->getDescription()); $this->assertSame('multilingual description', $person->translate('ru_RU')->getDescription()); }