0

我有两个实体:

付款和公司。

支付实体与公司实体有关联,如下所示:

/**
* @OneToOne(targetEntity="Companies")
* @JoinColumn(name="company_id", referencedColumnName="id")
*/
private $total;

为什么在插入时调用此关联?

我需要在付款表中设置一些东西(插入),但我遇到了错误。

Integrity constraint violation: 1048 Column 'company_id' cannot be null

欢迎任何帮助。

4

1 回答 1

0

您需要先获取(或创建)您的 $company 实体,然后在保存之前将其交给 $payment。就像是:

// First get the company that should be related to the new payment    
$company = $entityManager->getRepository('Company')->find($companyId);

// Attach it to our new payment
$payment->setCompany($company); // Or whatever the setter is in your entity class

// Now we can save
$entityManager->persist($payment);
$entityManager->flush();
于 2013-03-27T19:58:39.917 回答