在Doctrine2中,如何在多对多关系中进行翻译? Symfony2的

时间:2013-06-24 16:17:40

标签: symfony orm doctrine-orm many-to-many translation

我使用了Translatable和个人翻译;我已经实现了PysTranslation,我在PysRepository中使用了ORM查询提示。所有这一切都很好,问题出在我翻译实体Genero时。此实体与Pys实体具有多对多关系,并且上述方法不起作用。我如何翻译$genNombre实体的Genero属性?

/**
 * Pys
 *
 * @ORM\Table(name="pys")
 * @ORM\Entity(repositoryClass="Filmboot\PYSBundle\Entity\PysRepository")
 * @Gedmo\TranslationEntity(class="Filmboot\PYSBundle\Entity\PysTranslation")
 */
 class Pys
 {
/**
 * @var integer
 *
 * @ORM\Column(name="PYS_ID", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $pysId;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_STR", type="string", length=255, nullable=false)
 * @Gedmo\Translatable
 */
private $pysStr;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_TITULO", type="string", length=255, nullable=true)
 * @Gedmo\Translatable
 */
private $pysTitulo;

/**
 * @var integer
 *
 * @ORM\Column(name="PYS_DURACION", type="integer", nullable=true)
 */
private $pysDuracion;

/**
 * @var integer
 *
 * @ORM\Column(name="PYS_ANYO", type="integer", nullable=true)
 */
private $pysAnyo;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_PAIS", type="string", length=255, nullable=true)
 * @Gedmo\Translatable
 */
private $pysPais;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_SINOPSIS", type="string", length=3000, nullable=true)
 * @Gedmo\Translatable
 */
private $pysSinopsis;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_GUIONISTA", type="string", length=255, nullable=true)
 */
private $pysGuionista;

/**
 * @ORM\ManyToOne(targetEntity="Filmboot\DirectorBundle\Entity\Director", cascade={"remove"})
 * @ORM\JoinColumn(name="DIR_ID", referencedColumnName="DIR_ID", onDelete="CASCADE")
 */
private $director;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_IMAGEN", type="string", length=255, nullable=true)
 */
private $pysImagen;

/**
 * @var string
 *
 * @ORM\Column(name="PYS_IMAGEN_GRANDE", type="string", length=255, nullable=true)
 */
private $pysImagenGrande;

/**
 * @ORM\ManyToMany(targetEntity="\Filmboot\ActorBundle\Entity\Actor", mappedBy="peliculas", cascade={"remove"})
 */
private $actores;

/**
 * @ORM\ManyToMany(targetEntity="\Filmboot\PYSBundle\Entity\Genero", mappedBy="peliculas", cascade={"remove"})
 */
private $generos;

/**
 * @ORM\OneToMany(targetEntity="\Filmboot\PYSBundle\Entity\Premio", mappedBy="pys", cascade={"remove"})
 * @ORM\JoinColumn(name="PRE_ID", referencedColumnName="PRE_ID", onDelete="CASCADE")
 */
private $premios;

/**
 * @ORM\OneToMany(targetEntity="\Filmboot\UsuarioBundle\Entity\Voto", mappedBy="pys", cascade={"remove"})
 * @ORM\JoinColumn(name="PYS_ID", referencedColumnName="PYS_ID", onDelete="CASCADE")
 */
private $votaciones;


/**
 * @ORM\OneToMany(targetEntity="PysTranslation", mappedBy="object", cascade={"persist", "remove"})
 */
private $translations;

public function __construct() 
{
    $this->actores = new ArrayCollection();
    $this->generos = new ArrayCollection();
    $this->votaciones = new ArrayCollection();
    $this->translations = new ArrayCollection();
}

public function getTranslations()
{
    return $this->translations;
}

public function addTranslation(PysTranslation $t)
{
    if (!$this->translations->contains($t)) {
        $this->translations[] = $t;
        $t->setObject($this);
    }
}

这是Genero

<?php

namespace Filmboot\PYSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * Genero
 *
 * @ORM\Table(name="genero")
 * @ORM\Entity
 */
class Genero
{
/**
 * @var integer
 *
 * @ORM\Column(name="GEN_ID", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $genId;

/**
 * @var string
 *
 * @ORM\Column(name="GEN_NOMBRE", type="string", length=255, nullable=true)
 * @Gedmo\Translatable
 */
private $genNombre;

/**
 * @ORM\ManyToMany(targetEntity="\Filmboot\PYSBundle\Entity\Pys", inversedBy="generos")
 * @ORM\JoinTable(name="P_GENERO",
 *      joinColumns={@ORM\JoinColumn(name="GEN_ID", referencedColumnName="GEN_ID")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="PYS_ID", referencedColumnName="PYS_ID")}
 *      )
 */
 private $peliculas;

这是PysTranslation

/**
 * @ORM\Entity
 * @ORM\Table(name="pys_translation", uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={"locale", "object_id", "field"})})
 */
class PysTranslation extends AbstractPersonalTranslation
{
/**
 * Convinient constructor
 *
 * @param string $locale
 * @param string $field
 * @param string $value
 */
public function __construct($locale, $field, $value)
{
    $this->setLocale($locale);
    $this->setField($field);
    $this->setContent($value);
}

/**
 * @ORM\ManyToOne(targetEntity="Pys", inversedBy="translations")
 * @ORM\JoinColumn(name="object_id", referencedColumnName="PYS_ID", onDelete="CASCADE")
 */
protected $object;
}

这是PysRepository

class PysRepository extends EntityRepository
{

public function findPeliculas()
{
    $em = $this->getEntityManager();

    $consulta = $em->createQuery('
        SELECT p, a, d, g, pr, v
        FROM PYSBundle:Pys p
        JOIN p.actores a JOIN p.director d JOIN p.generos g JOIN p.premios pr JOIN p.votaciones v
        ORDER BY p.pysTitulo ASC
    ');

    return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getResult();
}

/**
 *
 * @param string $pysStr El slug de la película
 */
public function findPys($pysStr)
{
    $em = $this->getEntityManager();

    $consulta = $em->createQuery('
        SELECT p, a, d, g, pr, v
        FROM PYSBundle:Pys p
        JOIN p.actores a JOIN p.director d JOIN p.generos g JOIN p.premios pr JOIN p.votaciones v
        WHERE p.pysStr = :pysStr
    ');
    $consulta->setParameter('pysStr', $pysStr);

    return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getSingleResult();
}
}

1 个答案:

答案 0 :(得分:0)

我通过在Genero实体注释中添加此行来解决问题:

/**
 * Genero
 *
 * @ORM\Table(name="genero")
 * @ORM\Entity
 * @Gedmo\TranslationEntity(class="Filmboot\PYSBundle\Entity\GeneroTranslation")
 */
 class Genero