【问题标题】:Symfony2 - Doctrine Save entites with one to one relationSymfony2 - 具有一对一关系的 Doctrine Save 实体
【发布时间】:2011-12-01 15:46:27
【问题描述】:

我想保存已创建的表单,但出现错误。

我有 3 个实体:

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]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(namespace.person,CONSTRAINT FK_3370D440809EFCB0 FOREIGN KEY(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]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(db.T,CONSTRAINT FK_58C6694C2EF03101 FOREIGN KEY (information_id) REFERENCES Person (id))

任何帮助都会很酷

谢谢

【问题讨论】:

    标签: php symfony doctrine-orm mysql-error-1064


    【解决方案1】:

    我也遇到了这个问题,级联没有帮助,所以我最终只使用 oneToMany, manyToOne 关系。

    我的替代解决方案是不在生成的 SQL 中应用外键约束,但这会删除我的 --force 能力(并迫使我编写自己的脚本来忽略 FK 约束),所以 oneToMany 解决方案是对我来说更优雅。

    【讨论】:

      【解决方案2】:

      您正在尝试保存 Person,但您没有将与 Document(必须命名为 photo_profile)的关系指定为可为空,因此出现此错误。

      【讨论】:

      • 你的意思是我必须把: $entity->setPhotoProfile(new Document()); ?
      • 我的意思是我有完全相同的错误。 SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(namespace.person,CONSTRAINT FK_3370D440809EFCB0 FOREIGN KEY (photo_profile_id) REFERENCES Document (id))。我试过清缓存,还是一样
      • 这要好得多,谢谢。在将其设置为照片配置文件之前,您应该尝试保留文档。或者在关系上设置一个持久级联。
      • 你的意思是我必须持久化对象 P $this->persist($person);$entity->setInformation($person);$form = $this->createForm(new PostType( ), $实体); $form->bindRequest($request);按此顺序
      • 持久化 Document,持久化 Person,刷新,然后持久化 T。我看到你已经有级联了,所以奇怪的是这还没有工作。
      【解决方案3】:

      我在多对一关系中遇到了同样的问题;。我只需要:

      1. 保留所有者实体
      2. 刷新
      3. 创建逆并绑定它
      4. 坚持并再次刷新 然后一切正常!

      好吧,我无法解释,但我只是分享我的经验,也许它会起作用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        • 1970-01-01
        相关资源
        最近更新 更多