我有两个实体,产品和附属公司。我想插入一个新产品,但出现错误:
违反完整性约束:1048 列“affiliateId”不能为空
这是php代码:
$affiliate = $this->_em->getRepository("Common\Entity\Affiliate")->find(1);
$product = new Product();
$product->setAffiliate($affiliate);
$product->statusId = $form->getValue('statusId');
[...]
$product->created = new \DateTime("now");
$this->_em->persist($product);
$this->_em->flush();
产品实体:
<?php
namespace Common\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* A product.
*
* @ORM\Entity
* @ORM\Table(name="product")
*/
class Product {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="integer")
*/
protected $statusId;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\ManyToOne(targetEntity="Affiliate", inversedBy="product")
* @ORM\JoinColumn(name="affiliateId", referencedColumnName="id")
*/
protected $affiliate;
public function setAffiliate($affiliate) {
$this->affiliate = $affiliate;
}
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property) {
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
}
附属实体:
<?php
namespace Common\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* A product.
*
* @ORM\Entity
* @ORM\Table(name="affiliate")
*/
class Affiliate {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\Column(type="datetime")
*/
protected $lastModified;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="affiliate")
*/
protected $product;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
/**
* Magic getter to expose protected properties.
*
* @param string $property
* @return mixed
*/
public function __get($property) {
return $this->$property;
}
/**
* Magic setter to save protected properties.
*
* @param string $property
* @param mixed $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
}