{"id":1612,"date":"2022-08-30T15:17:59","date_gmt":"2022-08-30T15:17:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/using-doctrine-how-do-you-access-entity-methods-from-a-entityrepository-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:59","modified_gmt":"2022-08-30T15:17:59","slug":"using-doctrine-how-do-you-access-entity-methods-from-a-entityrepository-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-doctrine-how-do-you-access-entity-methods-from-a-entityrepository-collection-of-common-programming-errors\/","title":{"rendered":"Using Doctrine, how do you access Entity methods from a EntityRepository?-Collection of common programming errors"},"content":{"rendered":"<p>In the Symfony &#8220;Book&#8221;, they talk about Entities with references to other entities. Like, in my case, if I have a &#8220;Post&#8221; Entity with many &#8220;Comment&#8221; Entities referencing it, I can load the Post by its ID and then do $post-&gt;getComments().<\/p>\n<p>The Comment are lazy-loaded, it seems, and I would have to either go one-by-one through them, loading each (which is clearly wrong) or load them all in a separate query (which I could do, but I don&#8217;t know where, specifically, to put that query). The Book, however, suggests: &#8220;Of course, if you know up front that you&#8217;ll need to access both objects, you can avoid the second query by issuing a join in the original query.&#8221; It recommends I add a method to my (currently-empty) postRepository class. I do, and it looks like this:<\/p>\n<pre><code>public function loadPostFull($pid)\n{\n    return $this-&gt;getEntityManager()\n        -&gt;createQuery('\n            SELECT p, c FROM BUNDLENAME:Post p\n            JOIN p.comments c\n            WHERE p.id = :id')\n        -&gt;setParameter('id', $pid);\n    try {\n        return $query-&gt;getSingleResult();\n    } catch (\\Doctrine\\ORM\\NoResultException $e) {\n        return null;\n    }\n}\n<\/code><\/pre>\n<p>And then in my controller I do:<\/p>\n<pre><code>$fullPost = $this-&gt;getDoctrine()\n            -&gt;getRepository('BUNDLENAME:Post')\n            -&gt;loadPostFull($id);\n$id = $fullPost-&gt;getId();\n<\/code><\/pre>\n<p>But my simple request for $id gives a &#8220;Fatal error: Call to undefined method Doctrine\\ORM\\Query::getId()&#8221;. I know the controller is finding the loadPostFull method correctly, because if I typo the name it will fail there. It&#8217;s just not returning a proper Post entity. If I change the controller to call -&gt;find($id) instead of -&gt;loadPostFull($id) I can use all the internal methods of the Post id.<\/p>\n<p>The manual seems to indicate I can do that! What&#8217;s wrong?<\/p>\n<ol>\n<li>\n<p>I believe you have a typo :). Look at the first line of your code. You have a <strong>return<\/strong> statement! So you return the Query object and try { } catch() { } is never reached. You should put it like this:<\/p>\n<pre><code>$query = $this-&gt;getEntityManager()\n    -&gt;createQuery('\n        SELECT p, c FROM BUNDLENAME:Post p\n        JOIN p.comments c\n        WHERE p.id = :id')\n    -&gt;setParameter('id', $pid);\ntry {\n    return $query-&gt;getSingleResult();\n} catch (\\Doctrine\\ORM\\NoResultException $e) {\n    return null;\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:01:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In the Symfony &#8220;Book&#8221;, they talk about Entities with references to other entities. Like, in my case, if I have a &#8220;Post&#8221; Entity with many &#8220;Comment&#8221; Entities referencing it, I can load the Post by its ID and then do $post-&gt;getComments(). The Comment are lazy-loaded, it seems, and I would have to either go one-by-one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1612","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1612"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1612\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}