我有两个实体:
付款和公司。
付款实体与公司实体有关联,如下所示:
/**
* @OneToOne(targetEntity="Companies")
* @JoinColumn(name="company_id", referencedColumnName="id")
*/
private $total;
为什么要在插入时调用此关联?
我需要在付款表中设置一些内容(插入),我收到错误。
Integrity constraint violation: 1048 Column 'company_id' cannot be null
欢迎任何帮助。
答案 0 :(得分: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();