Doctrine在flush()上将有效参数设置为null

时间:2016-06-14 06:51:10

标签: php symfony doctrine-orm

我有两个多对一关系,来自$countryFrom的对象$countryToNotification获取列id_country_formid_country_to中的值在Country表中,id具有相同的值。 我有所有属性的setter和getter。

实体通知

class Notification
{
/**
 * @ORM\ManyToOne(targetEntity="Country")
 * @ORM\JoinColumn(name="id_country_from", referencedColumnName="id")
 */
private $countryFrom;

/**
 * @ORM\ManyToOne(targetEntity="Country")
 * @ORM\JoinColumn(name="id_country_to", referencedColumnName="id")
 */
private $countryTo;

/**
 * @var integer
 *
 * @ORM\Column(name="id_country_from", type="integer", nullable=false)
 */
private $idCountryFrom;

/**
 * @var integer
 *
 * @ORM\Column(name="id_country_to", type="integer", nullable=false)
 */
private $idCountryTo;   
}


实体国家/地区

class Country
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;  
}

但是,当我想从控制器在DB中插入数据时,与id_country_formid_country_to关联的属性将设置为null。


控制器方法

$notif = new Notification;
$notif->setCountryFrom(5); // SET to NULL 
$notif->setCountryTo(6); // SET to NULL       
$notif->setCreatedAt(new \DateTime());

$em->persist($notif);
$em->flush();


在persist()

之后

var_dump($ notification)

object(AppBundle\Entity\Notification)[457]
private 'countryFrom' => null
private 'countryTo' => null
private 'idCountryFrom' => int 5 // SET
private 'idCountryTo' => int 6   // SET    
private 'createdAt' => 
  object(DateTime)[448]
     public 'date' => string '2016-06-14 06:32:35.000000' (length=26)
     public 'timezone_type' => int 3
     public 'timezone' => string 'UTC' (length=3)
private 'id' => null

这里我们看到在persist()之后设置了idCurrencyFrom和idCurrencyTo,但是发生了下一个错误:

An exception occurred while executing 'INSERT INTO alerts (id_currency_from, id_currency_to, active, created_at) VALUES (?, ?, ?, ?)' with params [null, null, "2016-06-14 06:32:35"]:

在我设置多对一关系之前,代码工作正常。

1 个答案:

答案 0 :(得分:0)

我不能在不查看你的setter方法的情况下假设你应该将实体传递给setter方法而不是id。例如;

 $country = $em->getRepository('AppBundle:Country')->find(5);
 $notif->setCountry($country);