问题
I have two entites:
1) Service
/**
* @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) Lead
/**
* @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;
When doing select:
$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;
everything is great when I am doing select but when doing insert I am getting error:
Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'service_id' cannot be null
I guess this would be case also for update.
This error is ok, because service_id can not be null.
This is my insert code:
$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();
Please help.
回答1:
The problem starts here:
/** @Column(name="service_id", type="integer") */
private $service_id;
/**
* @ManyToOne(targetEntity="Service", inversedBy="leads")
* @JoinColumn(name="service_id", referencedColumnName="id")
*/
protected $service;
Never map a single db column as both @Column and @ManyToOne (or @OneToOne)! Doctrine2 will behave erratic when you do this, so you must choose between a regular column, or an association.
In this particular case I'd advise you to choose an association, so remove the @Column. Only use this part:
/**
* @ManyToOne(targetEntity="Service", inversedBy="leads")
* @JoinColumn(name="service_id", referencedColumnName="id")
*/
protected $service;
Then you can do something like this:
$service = $em->getRepository('Service')->find(1);
$lead = new Lead();
$lead->setService($service);
$em->persist($lead);
$em->flush();
Doctrine2 will make sure the primary key of the Service table will be used as foreign key (service_id) in the Lead table.
回答2:
I do not understand this. After a lot of googling I did this for insert:
1. $service = $this->em->getRepository("Entities\Service")->find($data['master-service-id']);
2. $lead->setService($service);
3. $lead->setService_id($data['master-service-id']);
I do not understand why do I have to do
$lead->setService($service); //this is from service entity
This is from mysql query log when I do $lead->setService($service); adn flush after that :
SELECT t0.id AS id1, t0.name AS name2, t0.description AS description3, t0.parent AS parent4 FROM services t0 WHERE t0.id = '5'
924 Query START TRANSACTION
924 Query INSERT INTO leads (user_id, service_id, lead_sent, date_created, date_sent, size, comment, estimate_date, source_zip, destination_zip, parent_lead) VALUES (19, 5, 0, '2013-04-30 19:53:48', '-0001-11-30 00:00:00', NULL, NULL, '2013-05-18 00:00:00', '21000', NULL, NULL)
924 Query commit
Any comment/answer is appreciated.
来源:https://stackoverflow.com/questions/16305633/doctrine-2-unable-to-insert-to-database-when-relation-is-present