Symfony ORM Integrity constraint issue -
i working towards following setup;
an article has many comments
a comment has 1 article
at minute successful in not allowing user insert comment database linked article_id not exist...
the problem comes when try print out table via inner join. receiving error:
notice: undefined index: article
here code in controller
$repository = $this->getdoctrine()->getrepository('appbundle:article'); $query = $repository->createquerybuilder('a') ->innerjoin('a.comments','c') ->where('a.title :phrase') ->setparameter(':phrase','hello') ->getquery();
and here entity classes - appreciated.
article entity
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; /** * @orm\entity * @orm\table(name="article") */ class article { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", length=100) * @assert\notblank() */ protected $title; /** * @orm\onetomany(targetentity="comment", mappedby="article") << error here beleive */ protected $comments; /** * @return mixed */ public function getid() { return $this->id; } /** * @param mixed $id */ public function setid($id) { $this->id = $id; } /** * @return mixed */ public function gettitle() { return $this->title; } /** * @param mixed $title */ public function settitle($title) { $this->title = $title; } /** * @return mixed */ public function getcomments() { return $this->comments; } /** * @param mixed $comments */ public function setcomments($comments) { $this->comments = $comments; }
}
comment entity
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use appbundle\entity\article; /** * @orm\entity * @orm\table(name="comment") */ class comment { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", length=300) */ protected $text; /** * @orm\column(type="integer") * @orm\manytoone(targetentity="article", inversedby="comment") * @orm\joincolumn(name="article_id", referencedcolumnname="id", ondelete="cascade") */ protected $article_id; /** * @return mixed */ public function getid() { return $this->id; } /** * @param mixed $id */ public function setid($id) { $this->id = $id; } /** * @return mixed */ public function gettext() { return $this->text; } /** * @param mixed $text */ public function settext($text) { $this->text = $text; } /** * @return mixed */ public function getarticleid() { return $this->article_id; } /** * @param mixed $article_id */ public function setarticleid($article_id) { $this->article_id = $article_id; } }
there 2 little mistakes in entities.
you can correct them way:
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; /** * @orm\entity * @orm\table(name="article") */ class article { // ... /** * @orm\onetomany(targetentity="comment", mappedby="article") */ protected $comments; }
and:
/** * @orm\entity * @orm\table(name="comment") */ class comment { // ... /** * @orm\manytoone(targetentity="article", inversedby="comments") * @orm\joincolumn(name="article_id", referencedcolumnname="id", ondelete="cascade") */ protected $article; }
here's changed:
mappedby="article"
→mappedby="article"
(it's other's entity property name, without accent)protected $article_id;
→protected $article;
(your property not id, entity)@orm\manytoone(targetentity="article", inversedby="comment")
→@orm\manytoone(targetentity="article", inversedby="comments")
(itcomments
property have inarticle
entity (with "s"), , target entity has uppercase a
Comments
Post a Comment