Doctrine将外键插入为NULL

时间:2018-03-14 19:11:34

标签: php doctrine-orm foreign-keys translation symfony-2.5

我有两个实体:ProfesionesProfesionesTranslation(一个Profesiones有很多ProfesionesTranslation)。当我尝试插入一组数据时,但是doctrine将我的外键插入为NULL。我认为映射可能是错误的。

Profesiones实体:

class Profesiones
{
    use ORMBehaviors\Translatable\Translatable;

     /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="profesiones_id_profesion_seq", allocationSize=1, initialValue=1)
     */
    private $idProfesion;

    /**
     * @var boolean
     *
     * @ORM\Column(name="activo", type="boolean", nullable=true)
     * @Assert\NotBlank(message="Campo Obligatorio.")
     */
    private $activo;

    /**
     * @var \SubgrupoServicios
     *
     * @ORM\ManyToOne(targetEntity="SubgrupoServicios")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_subgrupo", referencedColumnName="id_subgrupo")
     * })
     */
    private $idSubgrupo;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Empresas", mappedBy="idProfesion")
     */
    private $idEmpresa;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->idEmpresa = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getIdProfesion()
    {
        return $this->idProfesion;
    }

    /**
     * Set activo
     *
     * @param boolean $activo
     * @return Profesiones
     */
    public function setActivo($activo)
    {
        $this->activo = $activo;

        return $this;
    }

    /**
     * Get activo
     *
     * @return boolean 
     */
    public function getActivo()
    {
        return $this->activo;
    }

    /**
     * Set idSubgrupo
     *
     * @param \Sistema\FilmsBundle\Entity\SubgrupoServicios $idSubgrupo
     * @return Profesiones
     */
    public function setIdSubgrupo(\Sistema\FilmsBundle\Entity\SubgrupoServicios $idSubgrupo = null)
    {
        $this->idSubgrupo = $idSubgrupo;

        return $this;
    }

    /**
     * Get idSubgrupo
     *
     * @return \Sistema\FilmsBundle\Entity\SubgrupoServicios
     */
    public function getIdSubgrupo()
    {
        return $this->idSubgrupo;
    }

    /**
     * Add idEmpresa
     *
     * @param \Sistema\FilmsBundle\Entity\Empresas $idEmpresa
     * @return Profesiones
     */
    public function addIdEmpresa(\Sistema\FilmsBundle\Entity\Empresas $idEmpresa)
    {
        $this->idEmpresa[] = $idEmpresa;

        return $this;
    }

    /**
     * Remove idEmpresa
     *
     * @param \Sistema\FilmsBundle\Entity\Empresas $idEmpresa
     */
    public function removeIdEmpresa(\Sistema\FilmsBundle\Entity\Empresas $idEmpresa)
    {
        $this->idEmpresa->removeElement($idEmpresa);
    }

    /**
     * Get idEmpresa
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getIdEmpresa()
    {
        return $this->idEmpresa;
    }

}

ProfesionesTranslation实体:

class ProfesionesTranslation
{
    use ORMBehaviors\Translatable\Translation;

     /**
     * @var integer
     *
     * @ORM\Column(name="translatable_id", type="integer", nullable=false)
     */
    public $translatableId;

    /**
     * @var string
     *
     * @ORM\Column(name="profesion", type="string", length=100, nullable=true)
     * @Assert\NotBlank(message="Campo Obligatorio.")
     */
    private $profesion;

    /**
     * Set translatableId
     *
     * @param integer $translatableId
     * @return ProfesionesTranslation
     */
    public function setTranslatableId($translatableId)
    {
        $this->translatableId = $translatableId;

        return $this;
    }

    /**
     * Get translatableId
     *
     * @return integer 
     */
    public function getTranslatableId()
    {
        return $this->translatableId;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set profesion
     *
     * @param string $profesion
     * @return ProfesionesTranslation
     */
    public function setProfesion($profesion)
    {
        $this->profesion = $profesion;

        return $this;
    }

    /**
     * Get profesion
     *
     * @return string 
     */
    public function getProfesion()
    {
        return $this->profesion;
    }

    public function __toString()
    {
        return $this->getProfesion();
    }

}

控制器:

$servicio = new Profesiones();

$formulario = $this->createForm(new ServiciosType(), $servicio);

if($peticion->getMethod() =='POST'){

    $formulario->handleRequest($request);

    if ($formulario->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($servicio);
        $em->flush();

        $profesionesTranslation = new ProfesionesTranslation();

        $locale = 'es';
        $profesion = $formulario['profesion']->getData();
        $translatableId = $servicio->getIdProfesion();

        $profesionesTranslation->setLocale($locale);
        $profesionesTranslation->setProfesion($profesion);
        $profesionesTranslation->setTranslatableId($translatableId);

            $em->persist($profesionesTranslation);
            $em->flush();
...

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我假设您正在使用 KnpLabs DoctrineBehaviors可翻译documentation

根据文档,您不应直接管理 ProfesionesTranslation 实体,而应通过 Profesiones 实体进行管理。

更改控制器中的代码:

if ($formulario->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $profesion = $formulario['profesion']->getData();
    $locale = 'es';
    $servicio->translate($locale)->setProfesion($profesion);
    $em->persist($servicio);
    // In order to persist new translations, call mergeNewTranslations method, before flush
    $servicio->mergeNewTranslations();
    $em->flush();

从ProfesionesTranslation中删除$translatableId,不需要。 (不要忘记更新学说模式)

要获得翻译,请使用:

$profecion = $servicio->translate('es')->getProfesion();

使用" translate()"完成创建,设置或获取翻译实体method,我不会详细介绍其工作原理,如果需要,可以查看KnpLabs repo

相关问题