gpt4 book ai didi

php - Doctrine2 完整性约束违规 : 1048 while inserting 2 one-to-one assications

转载 作者:行者123 更新时间:2023-11-29 13:19:25 25 4
gpt4 key购买 nike

嗨,我得到了异常(exception):

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

当我想创建一个具有新联系人和地址关系的新人时。

Controller :

// 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 完整性约束违规 : 1048 while inserting 2 one-to-one assications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com