php - Symfony doctrine ManyToMany insert -


i 'm having form on can create new "book". book, can add tags (e.g. describe additional information it).

the relation between book , tag manytomany, because each book can have many tags, , each tag can rely different books. (each tag unique field on name). so, when user enters new tag book not exists in database, want create tag when submitting. if exists, want add tag book. i've tried following:

$book = $this->form->getdata();  foreach ($tags $tag) {   $tag = strtolower($tag);    // check if tag exists   $tagentity = $this->em->getrepository('bookbundle:tag')->findbyname($tag);    // if not, create new tag , add   if(null === $tagentity)   {       $tagentity = new tag();       $tagentity->setname($tag);   }    // add tag book   $book->addtag($tagentity);   // add book tag   $tagentity->addbook($book);    // create relation between tag , book   $this->em->persist($book);   $this->em->persist($tagentity);    $this->em->flush(); } 

questions:

1) first need create book after line 1 persist , flush, before can go on?

2) best way handle adding (new) tags books, described above ?

at moment when click on "submit", local apache not respond , "hangs up"..

regards

try not adding both book tag , tag book. add tag book , persist book entity. doctrine should all. , of course book or tag entity should have method capable method of adding book or tag generated doctrine automatically.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -