嗨,我得到了例外:
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
最佳答案
乍一看:name="id", referencedColumnName="contact_id"
是错误的处理方法。
http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional
关于php - 违反Doctrine2完整性约束:插入2个一对一关联时为1048,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21047931/