简体   繁体   English

主义将外键插入为NULL

[英]Doctrine insert foreign key as NULL

I have two entities: Profesiones and ProfesionesTranslation (one Profesiones have many ProfesionesTranslation). 我有两个实体: ProfesionesProfesionesTranslation (一个Profesiones具有许多ProfesionesTranslation)。 When I trying to insert a set of data, but doctrine insert my foreign key as NULL. 当我尝试插入一组数据时,但是在原则上,我将外键插入为NULL。 I suppose that mapping may be wrong. 我想映射可能是错误的。

Profesiones entity: 专业实体:

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 entity: 专业翻译实体:

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();
    }

}

Controller: 控制器:

$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();
...

Any help is greatly appreciated. 任何帮助是极大的赞赏。

I assume you are using KnpLabs DoctrineBehaviors translatable : documentation 我假设您正在使用可翻译的KnpLabs DoctrineBehaviors文档

According to the documentation, you should not directly manage the ProfesionesTranslation entity, but do it through Profesiones entity. 根据文档,您不应直接管理ProfesionesTranslation实体,而应通过Profesiones实体来进行管理。

Change the code in the controller: 更改控制器中的代码:

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();

Remove $translatableId from ProfesionesTranslation, not needed. 从ProfesionesTranslation中删除$translatableId ,不需要。 (Do not forget to update the doctrine schema) (不要忘了更新原则架构)

To get a translation, use: 要获得翻译,请使用:

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

Creating, setting or getting the translation entity is done using "translate()" method , I will not get into many detail on how it works, if you need you can check the KnpLabs repo . 创建,设置或获取翻译实体是使用“ translate()” 方法完成的,如果您需要查看KnpLabs仓库 ,我将不对其进行详细介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM