0

我有两个实体:

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();

请帮忙。

4

2 回答 2

2

问题从这里开始:

/** @Column(name="service_id", type="integer") */
private $service_id;

/**
 * @ManyToOne(targetEntity="Service", inversedBy="leads")
 * @JoinColumn(name="service_id", referencedColumnName="id")
 */
protected $service;

永远不要将单个 db 列同时映射为 @Column @ManyToOne(或@OneToOne)!当您这样做时,Doctrine2 将表现不稳定,因此您必须在常规列或关联之间进行选择。

在这种特殊情况下,我建议您选择一个关联,因此删除@Column。仅使用此部分:

/**
 * @ManyToOne(targetEntity="Service", inversedBy="leads")
 * @JoinColumn(name="service_id", referencedColumnName="id")
 */
protected $service;

然后你可以做这样的事情:

$service = $em->getRepository('Service')->find(1);

$lead = new Lead();
$lead->setService($service);

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

Doctrine2 将确保 Service 表的主键将用作 Lead 表中的外键 (service_id)。

于 2013-05-22T21:10:17.710 回答
-1

我不明白。经过大量谷歌搜索后,我为插入做了这个:

1. $service = $this->em->getRepository("Entities\Service")->find($data['master-service-id']);
2. $lead->setService($service);
3. $lead->setService_id($data['master-service-id']);

我不明白为什么我必须这样做

$lead->setService($service); //this is from service entity

这是我做 $lead->setService($service); 时的mysql查询日志。之后刷新:

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

任何评论/答案表示赞赏。

于 2013-04-30T17:53:19.480 回答