我有两个实体:
1) 服务
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue
*/
private $id;
/** @Column(length=255) */
private $name;
/** @Column(length=255) */
private $description;
/** @Column(type="integer") */
private $parent;
/**
* @OneToMany(targetEntity="Lead", mappedBy="service",cascade={"persist"})
**/
protected $leads;
2) 铅
/**
* @Id @Column(name="id", type="integer")
* @GeneratedValue
*/
private $id;
/** @Column(type="integer") */
private $user_id;
/** @Column(name="service_id", type="integer") */
private $service_id;
/** @Column(type="integer") */
private $lead_sent;
/** @Column(type="datetime") */
private $date_created;
/** @Column(type="datetime", nullable=true) */
private $date_sent = null;
/** @Column(type="integer", nullable=true) */
private $size;
/** @Column(type="text", nullable=true) */
private $comment;
/** @Column(type="datetime") */
private $estimate_date;
/** @Column(type="integer") */
private $source_zip;
/** @Column(type="integer", nullable=true) */
private $destination_zip;
/** @Column(type="integer", nullable=true) */
private $parent_lead;
/**
* @ManyToOne(targetEntity="Service", inversedBy="leads")
* * @JoinColumn(name="service_id", referencedColumnName="id")
**/
protected $service;
选择时:
$query = $em->createQuery("SELECT l FROM Entities\Lead l
LEFT JOIN l.service s
WHERE l.user_id='".$user_id."' ");
$leads = $query->getResult();
return $leads;
当我做选择时一切都很好,但是当我做插入时我收到错误:
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'service_id' cannot be null
我想这也是更新的情况。
这个错误是可以的,因为 service_id 不能为空。
这是我的插入代码:
$service = $this->em->getRepository("Entities\Service")->findBy(array("id"=>$data['master-service-id']));
$lead = new Entities\Lead;
$lead->setUser_id($this->user->getId());
$lead->setLead_sent(0);
$lead->setDate_created(new DateTime(date('Y-m-d H:i:s')));
$lead->setDate_sent(new DateTime(date('0000-00-00 00:00:00')));
$lead->setService_id($service[0]->getId());
if($data['commnet']){
$lead->setComment($data['commnet']);
}
$lead->setSource_zip($data['zip']);
if($data['target_zip']){
$lead->setDestination_zip($data['target_zip']);
}
$lead->setEstimate_date(new DateTime($data['date']));
$this->em->persist($lead);
$this->em->flush();
请帮忙。