我刚从Translatable DoctrineExtensions
安装了StofDoctrineExtensionsBundle
。
我有两个实体“空间”和“设备”,具有多对一的关系:
/**
* Space
* @ORM\Table(name="space")
* @ORM\Entity(repositoryClass="Project\ProjectBundle\Repository\PlaceRepository")
*/
class Space implements Translatable
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Project\ProjectBundle\Entity\Equipment", cascade={"persist"})
*/
private $equipments;
/**
* @var string
* @Gedmo\Translatable
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @Gedmo\Locale
*/
private $locale;
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
并且:
/**
* Equipment
* @ORM\Table("equipment")
* @ORM\Entity(repositoryClass="Project\ProjectBundle\Repository\EquipmentRepository")
*/
class Equipment implements Translatable
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Gedmo\Translatable
* @ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @Gedmo\Locale
*/
private $locale;
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
在我的获取查询功能spaceRepository
中,我有:
public function getSpace($id) {
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
$query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,$locale);
$db = $this
->createQueryBuilder('s')
->andWhere('s.id = :id')
->setParameter('id', $id)
->leftJoin('s.equipments', 'e')
->addSelect('e');
$query = $db->getQuery();
$query->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker');
$query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'en');
return $query->getSingleResult();
}
TranslationWalker应该在一个查询中获得所有结果,但它不起作用,最后,我对每个设备有一个查询(超过150个查询)。
如何在一个查询中获取所有已翻译的元素?
答案 0 :(得分:2)