无法在manyToOne关联上的Symfony中设置NULL值

时间:2014-09-11 22:33:44

标签: php symfony doctrine-orm

我正在使用Symfony2。我试图在ManyToOne关联上设置NULL值,我不断收到此错误:

  

ContextErrorException:Catchable Fatal Error:传递给WIC的参数1 \ SettlementBundle \ Entity \ SettlementReport :: setSupplierPayment()必须是WIC \ SupplierBundle \ Entity \ SupplierPayment的实例,null给定,在/ Applications / MAMP / htdocs /中调用第312行的ffss / src / WIC / SupplierBundle / Controller / SupplierPaymentController.php,在/Applications/MAMP/htdocs/ffss/src/WIC/SettlementBundle/Entity/SettlementReport.php第342行中定义

以下是我的settlementReport实体中的关联:

 /**
 * @ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\SupplierPayment", inversedBy="supplierReport")
 * @ORM\JoinColumn(name="supplierPayment_id", referencedColumnName="id", nullable=true)
 */
protected $supplierPayment;

以下是getter和setter方法:

 /**
 * Set supplierPayment
 *
 * @param \WIC\SupplierBundle\Entity\SupplierPayment $supplierPayment
 * @return SettlementReport
 */
public function setSupplierPayment(\WIC\SupplierBundle\Entity\SupplierPayment $supplierPayment)
{
    $this->supplierPayment = $supplierPayment;

    return $this;
}
/**
 * Get supplierPayment
 *
 * @return \WIC\SupplierBundle\Entity\SupplierPayment $supplierPayment
 */
public function getSupplierPayment()
{
    return $this->supplierPayment;
}

这是我的控制器,我试图设置一个NULL值:

 $settlementReport = $em->getRepository('WICSettlementBundle:SettlementReport')->find($srId);
 $settlementReport->setSupplierPayment(NULL);
 $em->flush($settlementReport);

为什么它会给我这个错误,如何将值设置为NULL?

由于

1 个答案:

答案 0 :(得分:4)

如果你真的想要设置NULL(显然是这种情况),只需按如下方式更改签名:

public function setSupplierPayment
    (\WIC\SupplierBundle\Entity\SupplierPayment $supplierPayment = null)

背景:PHP强制您传递类型转换类的对象,而不是其他任何东西;唯一的例外是null值,如果默认参数值也为空。