symfony2 - Symfony 2 - ManyToOne Bidirectional relationship behaviour -
i had big time trying figure out how setup manytoone -> onetomany relationship doctrine 2 , still not working...
here application behaviour:
a site haspages a user can write comment on page here entities (simplified):
comment entity:
** * @orm\entity * @orm\table(name="comment") */ class comment { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * many comments have 1 user * * @orm\manytoone(targetentity="\acme\userbundle\entity\user", inversedby="comments") */ protected $user; /** * many comments have 1 page * * @orm\manytoone(targetentity="\acme\pagebundle\entity\page", inversedby="comments") */ protected $page; ... /** * set user * * @param \acme\userbundle\entity\user $user * @return comment */ public function setuser(\acme\userbundle\entity\user $user) { $this->user = $user; homecoming $this; } /** * set page * * @param \acme\pagebundle\entity\page $page * @return comment */ public function setpage(\acme\pagebundle\entity\page $page) { $this->page = $page; homecoming $this; } user entity:
/** * @orm\entity * @orm\table(name="fos_user") */ class user extends baseuser { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * user create comment he's supposed owner of relationship * however, doctrine doc says: "the many side of onetomany/manytoone bidirectional relationships must owning * side", comment owner * * 1 user can write many comments * * @orm\onetomany(targetentity="acme\commentbundle\entity\comment", mappedby="user") */ protected $comments; ... /** * comments * * @return \doctrine\common\collections\collection */ public function getcomments() { homecoming $this->comments ?: $this->comments = new arraycollection(); } page entity:
/** * @orm\entity * @orm\table(name="page") */ class page { /** * @orm\id * @orm\column(type="integer") * @orm\generatedvalue(strategy="auto") */ protected $id; /** * 1 page can have many comments * owner comment * * @orm\onetomany(targetentity="\acme\commentbundle\entity\comment", mappedby="page") */ protected $comments; ... /** * @return \doctrine\common\collections\collection */ public function getcomments(){ homecoming $this->comments ?: $this->comments = new arraycollection(); } i want bidirectional relationship able collection of comments page or user (using getcomments()).
my problem is when seek save new comment, error saying doctrine not able create page entity. guess happening because it's not finding page (but should) it's trying create new page entity later link comment entity i'm trying create.
here method controller create comment:
public function createaction() { $user = $this->getuser(); $page = $this->getpage(); $comment = new entitycomment(); $form = $this->createform(new commenttype(), $comment); if ($this->getrequest()->getmethod() === 'post') { $form->bind($this->getrequest()); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $comment->setpage($page); $comment->setuser($user); $em->persist($comment); $em->flush(); homecoming $this->redirect($this->generateurl('acme_comment_listing')); } } homecoming $this->render('acmecommentbundle:default:create.html.twig', array( 'form' => $form->createview() )); } i don't understand why happening. i've checked page object in controller (returned $this->getpage() - homecoming object stored in session) , it's valid page entity exists (i've checked in db too).
i don't know , can't find having same problem :(
this exact error message have:
a new entity found through relationship 'acme\commentbundle\entity\comment#page' not configured cascade persist operations entity: acme\pagebundle\entity\page@000000005d8a1f2000000000753399d4. solve issue: either explicitly phone call entitymanager#persist() on unknown entity or configure cascade persist association in mapping illustration @manytoone(..,cascade={"persist"}). if cannot find out entity causes problem implement 'acme\pagebundle\entity\page#__tostring()' clue.
but don't want add together cascade={"persist"} because don't want create page on cascade, link existing one.
update1:
if fetch page before set it, it's working. still don't know why should.
public function createaction() { $user = $this->getuser(); $page = $this->getpage(); // fetch page repository $page = $this->getdoctrine()->getrepository('acmepagebundle:page')->findoneby(array( 'id' => $page->getid() )); $comment = new entitycomment(); // set relation manytoone $comment->setpage($page); $comment->setuser($user); $form = $this->createform(new commenttype(), $comment); if ($this->getrequest()->getmethod() === 'post') { $form->bind($this->getrequest()); if ($form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($comment); $em->flush(); homecoming $this->redirect($this->generateurl('acme_comment_listing')); } } homecoming $this->render('acmecommentbundle:default:create.html.twig', array( 'form' => $form->createview() )); } update2:
i've ended storing page_id in session (instead of total object) think improve thought considering fact won't have utilize session store id. i'm expecting doctrine cache query when retrieving page entity.
but can explain why not utilize page entity session? how setting session:
$pages = $site->getpages(); // homecoming doctrine collection if (!$pages->isempty()) { // set first page of collection in session $session = $request->getsession(); $session->set('page', $pages->first()); }
actually, page object not known entity manager, object come session. (the right term "detached" entity manager.) that's why tries create new one.
when object different source, have utilize merge function. (from session, unserialize function, etc...)
instead of
// fetch page repository $page = $this->getdoctrine()->getrepository('acmepagebundle:page')->findoneby(array( 'id' => $page->getid() )); you can utilize :
$page = $em->merge($page); it help if want work object in session.
more info on merging entities here
symfony2 doctrine2 one-to-many many-to-one
No comments:
Post a Comment