Php 原则2,使用外键插入表时出错
我是一个非常新的使用条令的人,这是我使用它的第一个项目,当我尝试插入一个新用户时,我遇到了一个错误 问题是我有一个具有外键国家/地区的类用户,当我尝试插入一个用户原则时,也尝试插入该国家/地区,该国家/地区已经存在,因此PDO将启动一个完整性约束冲突,并将原则\DBAL\DBALException作为原则 我知道注释cascade={“persist”}会使要在db中写入的国家实体出现另一个错误:Php 原则2,使用外键插入表时出错,php,mysql,doctrine-orm,Php,Mysql,Doctrine Orm,我是一个非常新的使用条令的人,这是我使用它的第一个项目,当我尝试插入一个新用户时,我遇到了一个错误 问题是我有一个具有外键国家/地区的类用户,当我尝试插入一个用户原则时,也尝试插入该国家/地区,该国家/地区已经存在,因此PDO将启动一个完整性约束冲突,并将原则\DBAL\DBALException作为原则 我知道注释cascade={“persist”}会使要在db中写入的国家实体出现另一个错误: A new entity was found through the relationship '
A new entity was found through the relationship 'User#country' that was not configured to cascade persist operations for entity: Country@0000000078b1861f00007f935266d9fe. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Country#__toString()' to get a clue.
我尝试了所有级联选项,只尝试了persist,上面所有的错误都没有出现
是否有类似cascade={“no persist”}的内容,或者有类似的内容告诉我们必须在表country中插入此属性的值
一些代码:
/**
* User
*
* @Table(name="user")
* @Entity
*/
class User {
...
/**
* @var Country
*
* @OneToOne(targetEntity="Country", cascade={"persist"})
* @JoinColumn(name="country", referencedColumnName="id")
*/
private $country
...
}
/**
* Country
*
* @Table(name="country")
* @Entity
*/
class Country {
...
/**
* @var integer
*
* @Column(name="id", type="integer")
* @Id
*/
private $id;
}
任何线索都将不胜感激。
谢谢。将cascade=persist放回 你需要检查数据库,看看这个国家是否存在。插入现有国家/地区失败,因为该国家/地区对象需要由实体管理器管理
$country = $countryRepository->find($countryId);
if (!$country)
{
$country = new Country();
$entityManager->persist($country);
}
$user->setCountry($country);
可能没有定义表别名。如果表别名是:@table(name=“country”),那么它已经定义了…谢谢!我最终将国家类型更改为整数并删除了所有关系,您的方式更简单、更好,再次感谢。