我想保存我已经创建的表单,但我有一个错误。
我有三个实体
class T {
/**
* @ORM'OneToOne(targetEntity="MyNameSpace'ProfileBundle'Entity'Person", cascade={"persist"})
*/
private $information;
}
class Person {
/**
* @ORM'OneToOne(targetEntity="MyNameSpace'MediaBundle'Entity'Document", cascade={"persist"}))
*/
private $photo_profile;
}
class Document
{
private $file;
}
当我用下面的代码保存我的"T"类时:
public function createAction()
{
$entity = new T();
$request = $this->getRequest();
$form = $this->createForm(new TType(), $entity);
$form->bindRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();
}
}
我有这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1452不能添加或更新子行:a foreign key constraint fails (
namespace
. c .)person
,约束FK_3370D440809EFCB0
外键(photo_profile_id
)引用Document
(id
))
任何帮助都会很酷
谢谢大家
下面是我的代码:
/**
* Creates a new Tutor entity.
*/
public function createAction()
{
$entity = new T();
$person = new Person();
$document = new Document();
$person->setPhoto($document);
$entity->setInformation($person);
$request = $this->getRequest();
$form = $this->createForm(new TType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity->getInformation()->getPhoto());
$em->persist($entity->getInformation());
$em->flush();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('Index'));
}
和我有这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1452不能添加或更新子行:a foreign key constraint fails (
db
. 0)。T
,约束FK_58C6694C2EF03101
外键(information_id
)引用Person
(id
)
任何帮助都将是很酷的
谢谢
我也遇到了这个问题,级联没有帮助,所以我最终只是使用oneToMany, manyToOne关系代替。
我的替代解决方案是在生成的SQL中不应用外键约束,但这将删除我的——force能力(并迫使我编写自己的脚本来忽略FK约束),因此oneToMany解决方案对我来说更优雅。
您正在尝试保存Person
,但您没有指定与Document(必须命名为photo_profile)的关系为空,因此出现此错误
我在ManyToOne关系中遇到了同样的问题。我必须:
- 保存所有者实体
- 冲洗
- 创建反向并绑定
- 保持并再次刷新然后一切正常了!
好吧,我不能解释,但我只是分享我的经验,也许它会服务!