2014-01-10 24 views
0

嗨,我得到異常:Doctrine2完整性約束違規:1048同時插入2一到一個assications

INSERT INTO persons (contact_id, addresses_id, files_id, firstname, middlename, lastname, gender, birthday, cdate, udate, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [2, null, null, "Foo", "", "Bar", 1, null, "2014-01-10 15:59:45", null, null]: 
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'addresses_id' cannot be null 

當我想創建一個新的人新的聯繫方式和地址的關係。

控制器:

// Person Contact 
$contact = new Contact(); 
$contact->populate($contactData); 
$em->persist($contact); 

// Person Address 
$address = new Address(); 
$address->populate($addressData); 
$countryRef = $em->getReference('Admin\Entity\Country', $address->getCountriesId()); 
$address->setCountry($countryRef); 
$em->persist($address); 

$person = new Person(); 
$person->populate($personData); 
$person->setContact($contact); 
$person->setAddress($address); 

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

地址型號:

class Address implements InputFilterAwareInterface 
{ 
/** 
* Instance of InputFilterInterface. 
* 
* @var InputFilter 
*/ 
private $inputFilter; 

/** 
* @var integer 
* @ORM\Id 
* @ORM\Column(name="id", type="integer", nullable=false) 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @var integer 
* @ORM\Column(name="countries_id", type="integer", nullable=false) 
*/ 
protected $countriesId; 

// Removed not important fields 

/** 
* @var datetime 
* @ORM\Column(name="cdate", type="datetime", nullable=true) 
*/ 
protected $cdate; 

/** 
* @var datetime 
* @ORM\Column(name="udate", type="datetime", nullable=true) 
*/ 
protected $udate; 

/** 
* @var \Admin\Entity\Company 
* @ORM\OneToOne(targetEntity="\Admin\Entity\Company", mappedBy="address") 
* @ORM\JoinColumn(name="id", referencedColumnName="addresses_id", nullable=false) 
*/ 
protected $company; 

/** 
* @var \Admin\Entity\Person 
* @ORM\OneToOne(targetEntity="\Admin\Entity\Person", mappedBy="address") 
* @ORM\JoinColumn(name="id", referencedColumnName="addresses_id", nullable=false) 
*/ 
protected $person; 

/** 
* @var \Admin\Entity\Country 
* @ORM\ManyToOne(targetEntity="\Admin\Entity\Country", inversedBy="addresses") 
* @ORM\JoinColumn(name="countries_id", referencedColumnName="id", nullable=false) 
*/ 
protected $country; 

聯繫型號:

class Contact implements InputFilterAwareInterface 
{ 
/** 
* Instance of InputFilterInterface. 
* 
* @var InputFilter 
*/ 
private $inputFilter; 

/** 
* @var integer 
* @ORM\Id 
* @ORM\Column(name="id", type="integer", nullable=false) 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* Primary email address 
* 
* @var string 
* @ORM\Column(name="email", type="string", length=100, nullable=true, options={"comment"="Primary email address"}) 
*/ 
protected $email; 


// Removed not important fields 

/** 
* @var datetime 
* @ORM\Column(name="cdate", type="datetime", nullable=true) 
*/ 
protected $cdate; 

/** 
* @var datetime 
* @ORM\Column(name="udate", type="datetime", nullable=true) 
*/ 
protected $udate; 

/** 
* @var \Admin\Entity\CompaniesHasPerson 
* @ORM\OneToOne(targetEntity="\Admin\Entity\CompaniesHasPerson", mappedBy="contact") 
* @ORM\JoinColumn(name="id", referencedColumnName="contact_id", nullable=false) 
*/ 
protected $companiesHasPerson; 

/** 
* @var \Admin\Entity\Company 
* @ORM\OneToOne(targetEntity="\Admin\Entity\Company", mappedBy="contact") 
* @ORM\JoinColumn(name="id", referencedColumnName="contact_id", nullable=false) 
*/ 
protected $company; 

/** 
* @var \Admin\Entity\Person 
* @ORM\OneToOne(targetEntity="\Admin\Entity\Person", mappedBy="contact") 
* @ORM\JoinColumn(name="id", referencedColumnName="contact_id", nullable=false) 
*/ 
protected $person; 

而且奇怪的事實是,當我切換地址模型與接觸模型和先堅持地址模式,教條會拋出錯誤:

An exception occurred while executing 'INSERT INTO persons (contact_id, addresses_id, files_id, firstname, middlename, lastname, gender, birthday, cdate, udate, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, 2, null, "Foo", "", "Bar", 1, null, "2014-01-10 16:16:50", null, null]: 
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'contact_id' cannot be null 

回答

相關問題