Doctrine Cascade Options for OneToMany

后端 未结 2 893
粉色の甜心
粉色の甜心 2020-12-24 05:21

I\'m having a hard time making sense of the Doctrine manual\'s explanation of cascade operations and need someone to help me understand the options in terms of a simple Many

2条回答
  •  不知归路
    2020-12-24 06:06

    If you have a @OneToMany unidirectional association, like that described in section 6.10 of the Doctrine Reference, then most likely you forgot to persist the Topic before calling flush. Don't set the topic_id primary key in Article. Instead set the Topic instance.

    For example, given Article and Topic entities like these:

    text = $text;
        }
     }
     public function setArticle($text)
     {
         $this->text = $text;
     }
    
     public function setTopic(Topic $t)
    {
         $this->topic = $t;
    }
    } 
    
    id;}
    }
    

    After you generate the schema:

    # doctrine orm:schema-tool:create
    

    your code to persist these entities would look like something this

    //configuration omitted..
    $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
    
    $topic = new Entities\Topic();
    $article1 = new Entities\Article("article 1");
    $article2 = new Entities\Article("article 2");
    $article1->setTopic($topic);
    $article2->setTopic($topic);
    $em->persist($article1);
    $em->persist($article2);
    $em->persist($topic);
    
    try {
        $em->flush();
    } catch(Exception $e) {
        $msg= $e->getMessage();
        echo $msg . "
    \n"; } return;

    I hope this helps.

提交回复
热议问题