Php 原则2,插入有关联的实体

Php 原则2,插入有关联的实体,php,zend-framework,doctrine,Php,Zend Framework,Doctrine,我有两个实体: 付款和公司 支付实体与公司实体的关联如下: /** * @OneToOne(targetEntity="Companies") * @JoinColumn(name="company_id", referencedColumnName="id") */ private $total; 为什么在insert上调用此关联 我需要设置一些东西(插入)到付款表,我得到的错误 Integrity constraint violation: 1048 Column 'company_id'

我有两个实体:

付款和公司

支付实体与公司实体的关联如下:

/**
* @OneToOne(targetEntity="Companies")
* @JoinColumn(name="company_id", referencedColumnName="id")
*/
private $total;
为什么在insert上调用此关联

我需要设置一些东西(插入)到付款表,我得到的错误

Integrity constraint violation: 1048 Column 'company_id' cannot be null
欢迎提供任何帮助。

您需要先获取(或创建)您的$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();

谢谢,我会试试这个,让你们知道结果。我只是不明白为什么我要这么做?我的意思是,有了这个协会,我只想做一个选择,你能解释我为什么要设置$company吗?你只想做一个选择吗?你最初的问题是“为什么这个协会被称为insert?”我很困惑。如果您正在执行select,则条令应该从数据库中获取现有的公司关系。如果要插入/保存,则必须为Doctrine提供要保存的关系。有意义吗?我之所以这样做是因为选择。为什么在insert上调用它?这就是我的问题。您的数据库是否可能不允许在company_id列中使用空值?