繁体   English   中英

Symfony / Doctrine在多对一关系中将null作为外键插入

[英]Symfony/Doctrine inserts null as foreign key in many to one relationship

我有两个实体, LeagueClub ,可以预见一个League可以有很多Club 只是为了测试一下我试图将一个名为test的俱乐部插入联盟,ID为1(数据库中确实存在,这不是问题)。 如果我执行以下操作,那么它可以正常工作:

$club = new Club();
$club->setName("test");
$league = $this->getDoctrine()->getRepository("AppBundle:League")->find(1);
$club->setLeague($league);
$em->persist($club);
$em->persist($league);
$em->flush();

但是,如果我将第四行改为$league->addClub($club); 然后它在数据库中插入一个null值作为league_id

我为Club实体提供以下代码:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Club
 *
 * @ORM\Table(name="club")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ClubRepository")
 */
class Club
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity="League", inversedBy="clubs")
     * @ORM\JoinColumn(name="league_id", referencedColumnName="id")
     */
    private $league;


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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Club
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set league
     *
     * @param \AppBundle\Entity\League $league
     *
     * @return Club
     */
    public function setLeague(\AppBundle\Entity\League $league = null)
    {
        $this->league = $league;

        return $this;
    }

    /**
     * Get league
     *
     * @return \AppBundle\Entity\League
     */
    public function getLeague()
    {
        return $this->league;
    }
}

以及League实体的以下内容:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * League
 *
 * @ORM\Table(name="league")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\LeagueRepository")
 */
class League
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity="Club", mappedBy="league")
     */
    private $clubs;


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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return League
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->clubs = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add club
     *
     * @param \AppBundle\Entity\Club $club
     *
     * @return League
     */
    public function addClub(\AppBundle\Entity\Club $club)
    {
        $this->clubs[] = $club;

        return $this;
    }

    /**
     * Remove club
     *
     * @param \AppBundle\Entity\Club $club
     */
    public function removeClub(\AppBundle\Entity\Club $club)
    {
        $this->clubs->removeElement($club);
    }

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

我已经检查了数据库模式是否符合doctrine:schema:update ,我似乎尝试了持久和冲洗的每一个组合......在联盟之前坚持俱乐部,在俱乐部之前坚持联盟,坚持只是联盟,坚持俱乐部,坚持联盟然后冲洗然后坚持俱乐部然后再次冲洗......没有任何工作。

Doctrine只会检查协会的拥有方是否有变更。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html

您可以像这样将更改级联到拥有方

// AppBundle\Entity\League.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * League
 *
 * @ORM\Table(name="league")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\LeagueRepository")
 */
class League
{

    // ...

    /**
     * Add club
     *
     * @param \AppBundle\Entity\Club $club
     *
     * @return League
     */
    public function addClub(\AppBundle\Entity\Club $club)
    {
        $this->clubs[] = $club;
        $club->setLeague($this);

        return $this;
    }

    // ...
}

问题来自你的addClub()方法。 当你在联盟中添加一个俱乐部时,你必须告诉俱乐部它所添加的联赛。

你的方法应该是这样的

public function addClub(Club $club)
{
    $this-cubs[] = $club;
    $club->setLeague($this); // <-- there, tell the club in which league it is

    return $this;
}

暂无
暂无

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

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