简体   繁体   English

Symfony2 - Doctrine以一对一的关系保存entites

[英]Symfony2 - Doctrine Save entites with one to one relation

I want to save a form that I have created but I have an error. 我想保存一个我创建的表单,但是我有一个错误。

I have 3 entities: 我有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;
}

When I save my "T" class with this code : 当我用这段代码保存我的“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();
        }
} 

I have this error: 我有这个错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( namespace . person , CONSTRAINT FK_3370D440809EFCB0 FOREIGN KEY ( photo_profile_id ) REFERENCES Document ( id )) SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败( namespaceperson ,CONSTRAINT FK_3370D440809EFCB0 FOREIGN KEY( photo_profile_id )REFERENCES Documentid ))

Any helps will be cool 任何帮助都会很酷

Thank you for all 感谢你所做的一切


Here the code I have: 这里是我的代码:

/**
 * 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'));
    }

and I have this error: 我有这个错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( db . T , CONSTRAINT FK_58C6694C2EF03101 FOREIGN KEY ( information_id ) REFERENCES Person ( id )) SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败( dbT ,约束FK_58C6694C2EF03101外键( information_id )引用Personid ))

Any help please will be cool 任何帮助请冷静

Thanks 谢谢

I ran into this issue as well and cascading didn't help so I ended up just using oneToMany, manyToOne relations instead. 我也遇到了这个问题并且级联没有帮助,所以我最终只使用了oneToMany,manyToOne关系。

The alternative solution I had was to not apply the foreign key constraints in the generated SQL but that would remove my --force ability (and force me to write my own script to ignore the FK constraints), so the oneToMany solution was more elegant for me. 我的替代解决方案是不在生成的SQL中应用外键约束,但这将删除我的--force能力(并强迫我编写自己的脚本以忽略FK约束),因此oneToMany解决方案更优雅我。

I had the same issue with a ManyToOne relation;. 我遇到了与ManyToOne关系相同的问题; I just had to : 我只需要:

  1. persist the owner entity 坚持所有者实体
  2. flush 红晕
  3. create the inverse and bind it 创建逆并绑定它
  4. persist and flush again Then everything went normal ! 坚持和再次冲洗然后一切正常!

Well I can't explain that but I just share my experience and maybe it will serve ! 好吧,我无法解释,但我只是分享我的经验,也许它会服务!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM