我有两个实体League
和Club
,可以预测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);
,那么它会在数据库中插入一个空值作为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
的数据有关,而且我似乎已经尝试了持续和冲洗的所有组合......在联盟之前坚持俱乐部,坚持在俱乐部之前的联赛,坚持联盟,坚持俱乐部,坚持联盟然后冲洗然后坚持俱乐部然后再次冲洗......没有任何效果。
答案 0 :(得分:0)
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;
}
// ...
}
答案 1 :(得分:0)
问题来自您的addClub()
方法。当你在联盟中添加一个俱乐部时,你必须告诉俱乐部它在哪个联赛中被添加。
您的方法应如下所示
public function addClub(Club $club)
{
$this-cubs[] = $club;
$club->setLeague($this); // <-- there, tell the club in which league it is
return $this;
}