Acudo ya desesperando despues de mirar mil sitios a ver si a alguien le ha pasado o sabe la solucion a un problema muy estupido.
La inserccion y el guardado de la traducciones me las hace perfectamente. El caso es que cuando obtengo el objeto mediante un find quiero que este venga traducido automaticamente (Se que poniendo un refresh seteando el locale funcionaria) pero no se puede hacer esto automaticamente???
Tengo lo siguiente (Entidad/Documento)
Código PHP:
<?php
namespace MGDExampleBundleDocument;
use DoctrineODMMongoDBMappingAnnotations as ODM;
use GedmoMappingAnnotation as Gedmo;
use GedmoTranslatableTranslatable;
/**
* MGD\ExampleBundle\Document\Student
*
* @ODM\Document
* @ODM\ChangeTrackingPolicy("DEFERRED_IMPLICIT")
*/
class Student implements Translatable
{
/**
* @var id $id
*
* @ODM\Id(strategy="AUTO")
*/
protected $id;
/**
* @var string $name
*
* @Gedmo\Translatable
* @ODM\Field(name="name", type="string")
*/
protected $name;
/**
* @var timestamp $birth_date
*
* @ODM\Field(name="birth_date", type="timestamp")
*/
protected $birth_date;
/**
* @var string $nationality
*
* @Gedmo\Translatable
* @ODM\String
*/
protected $nationality;
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string $name
*/
public function getName()
{
return $this->name;
}
/**
* Set birthDate
*
* @param timestamp $birthDate
* @return self
*/
public function setBirthDate($birthDate)
{
$this->birth_date = $birthDate;
return $this;
}
/**
* Get birthDate
*
* @return timestamp $birthDate
*/
public function getBirthDate()
{
return $this->birth_date;
}
/**
* Set nationality
*
* @param string $nationality
* @return self
*/
public function setNationality($nationality)
{
$this->nationality = $nationality;
return $this;
}
/**
* Get nationality
*
* @return string $nationality
*/
public function getNationality()
{
return $this->nationality;
}
}
Código PHP:
<?php
namespace MGDExampleBundleDataFixtures;
use DoctrineCommonDataFixturesFixtureInterface;
use DoctrineCommonPersistenceObjectManager;
use MGDExampleBundleDocumentStudent;
class StudentData implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$student= new Student();
$student
->setName("Mikel")
->setNationality("English Default")
->setLocale('en')
;
$manager->persist($student);
$manager->flush();
$student
->setName("Miguel")
->setNationality("Español")
->setLocale('es')
;
$manager->persist($student);
$manager->flush();
$student
->setName("EaterFrankFurT")
->setNationality("German1en")
->setLocale('de')
;
$manager->persist($student);
$manager->flush();
// Or you can use this
$repository = $manager->getRepository('Gedmo\\Translatable\\Document\\Translation');
$student= new Student();
$repository
->translate($student,'name', 'es','Pechitos es')
->translate($student,'name', 'de','DrugenFength de')
->translate($student,'name', 'en','Nationality en')
->translate($student,'nationality', 'es','nationality es')
->translate($student,'nationality', 'de','nationality de')
->translate($student,'nationality', 'en','nationality en')
;
$manager->persist($student);
$manager->flush();
}
}
Código PHP:
class DefaultController extends Controller
{
/**
* @Route("/hello/{name}")
* @Template()
*/
public function indexAction($name)
{
/** @var Request $request */
$request = $this->container->get('request');
$request->setLocale('de');
/** @var Student $studentReturned */
$manager = $this->get('doctrine_mongodb')->getManager();
$studentReturned=$manager->getRepository("MGDExampleBundle:Student")->find("528f8156d82fcc5425000008");
/* Opción que no querria usar
$studentReturned->setLocale($request->getLocale());
$manager->refresh($studentReturned);*/
$translations=null;
return array('translations' => $translations, 'student' => $studentReturned );
}
}