当我尝试保存订单实体时,出现以下错误:
完整性约束违规:1048 列“package_id”不能为空。
简化实体:
class Order
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="DeliveryPackage",inversedBy="order", cascade={"persist"})
* @ORM\JoinColumn (name="package_id", referencedColumnName="id")
*/
protected $package;
/**
* @ORM\ManyToOne(targetEntity="User",cascade={"persist"} )
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setPackage($package)
{
$this->package = $package;
}
public function getPackage()
{
return $this->package;
}
}
class DeliveryPackage
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Order",mappedBy="deliverypackage",cascade={"persist"})
* @ORM\JoinColumn (name="order_id", referencedColumnName="id")
*/
protected $order;
public function setId( $id )
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setOrder( $order )
{
$this->order = $order;
}
public function getOrder()
{
return $this->order;
}
}
要点是,这两个实体都是新的。所以他们还没有身份证。有什么选择,可以同时保存它们
$em->persist( $order );
$em->flush();
?