{"id":1625,"date":"2022-08-30T15:18:05","date_gmt":"2022-08-30T15:18:05","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/doctrine-custom-entity-repository-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:05","modified_gmt":"2022-08-30T15:18:05","slug":"doctrine-custom-entity-repository-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/doctrine-custom-entity-repository-collection-of-common-programming-errors\/","title":{"rendered":"Doctrine: custom entity repository-Collection of common programming errors"},"content":{"rendered":"<p>Have some problems with building custom entity repository.<\/p>\n<p>I have the following fatal error while trying method of custom entity repository<\/p>\n<pre><code>Fatal error: Uncaught exception 'BadMethodCallException' with message 'Undefined method 'getByParentId'. The method name must start with either findBy or findOneBy!' in C:\\Users\\user\\Desktop\\projects\\interview\\application\\libraries\\Doctrine\\ORM\\EntityRepository.php:215 Stack trace: #0 C:\\Users\\user\\Desktop\\projects\\interview\\application\\controllers\\CommentController.php(58): Doctrine\\ORM\\EntityRepository-&gt;__call('getByParentId', Array) #1 C:\\Users\\user\\Desktop\\projects\\interview\\application\\controllers\\CommentController.php(58): Doctrine\\ORM\\EntityRepository-&gt;getByParentId('1') #2 [internal function]: CommentController-&gt;viewCommentsListByParentId('1') #3 C:\\Users\\user\\Desktop\\projects\\interview\\system\\core\\CodeIgniter.php(359): call_user_func_array(Array, Array) #4 C:\\Users\\user\\Desktop\\projects\\interview\\index.php(203): require_once('C:\\Users\\user\\D...') #5 {main} thrown in C:\\Users\\user\\Desktop\\projects\\interview\\application\\libraries\\Doctrine\\ORM\\EntityRepository.php on line 215\n<\/code><\/pre>\n<p>My project structure is models Mappings Repositories Entities Proxies<\/p>\n<p>My doctrine.php has repository loader like this<\/p>\n<pre><code>\/\/ load the repositories\n$repositoryClassLoader = new \\Doctrine\\Common\\ClassLoader('Repositories', APPPATH.'models');\n$repositoryClassLoader-&gt;register();\n<\/code><\/pre>\n<p>My model class has the following declaration<\/p>\n<pre><code>namespace Entities;\n\nuse Doctrine\\ORM\\Mapping as ORM;\n\n\/**\n * Entities\\Comment\n * @Entity (repositoryClass=\"Repositories\\CommentRepository\")\n *\/\nclass Comment\n{\n<\/code><\/pre>\n<p>My Repository class has the following declaration<\/p>\n<pre><code>namespace Repositories;\nuse Doctrine\\ORM\\EntityRepository;\nuse Entities;\n\nclass CommentRepository extends EntityRepository {\n    public function getByParentId($parentid) {\n        return parent::findBy(array('parentid' =&gt; $parentid), array('creationdate' =&gt; 'DESC'));\n    }\n}\n<\/code><\/pre>\n<p>Controller code calling for methods of custom repository<\/p>\n<pre><code>   public function viewCommentsListByParentId($parentid) {\n        $data = array();\n        $data ['comment'] = $this-&gt;em-&gt;getRepository('Entities\\Comment')-&gt;getByParentId($parentid);\n\n        $this-&gt;load-&gt;view('commentsList', $data);\n    } \n<\/code><\/pre>\n<p>What I&#8217;m doing wrong??? Please, help.<\/p>\n<p>object(Doctrine\\ORM\\Mapping\\ClassMetadata)#34 (32) { [&#8220;name&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; [&#8220;namespace&#8221;]=&gt; string(8) &#8220;Entities&#8221; [&#8220;rootEntityName&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; [&#8220;customRepositoryClassName&#8221;]=&gt; NULL [&#8220;isMappedSuperclass&#8221;]=&gt; bool(false) [&#8220;parentClasses&#8221;]=&gt; array(0) { } [&#8220;subClasses&#8221;]=&gt; array(0) { } [&#8220;namedQueries&#8221;]=&gt; array(0) { } [&#8220;identifier&#8221;]=&gt; array(1) { [0]=&gt; string(2) &#8220;id&#8221; } [&#8220;inheritanceType&#8221;]=&gt; int(1) [&#8220;generatorType&#8221;]=&gt; int(4) [&#8220;fieldMappings&#8221;]=&gt; array(11) { [&#8220;id&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(2) &#8220;id&#8221; [&#8220;type&#8221;]=&gt; string(7) &#8220;integer&#8221; [&#8220;id&#8221;]=&gt; bool(true) [&#8220;columnName&#8221;]=&gt; string(2) &#8220;id&#8221; } [&#8220;parentid&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(8) &#8220;parentid&#8221; [&#8220;type&#8221;]=&gt; string(7) &#8220;integer&#8221; [&#8220;columnName&#8221;]=&gt; string(8) &#8220;parentid&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;isactive&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(8) &#8220;isactive&#8221; [&#8220;type&#8221;]=&gt; string(7) &#8220;integer&#8221; [&#8220;columnName&#8221;]=&gt; string(8) &#8220;isactive&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;isremoved&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(9) &#8220;isremoved&#8221; [&#8220;type&#8221;]=&gt; string(7) &#8220;integer&#8221; [&#8220;columnName&#8221;]=&gt; string(9) &#8220;isremoved&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;removaldate&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(11) &#8220;removaldate&#8221; [&#8220;type&#8221;]=&gt; string(8) &#8220;datetime&#8221; [&#8220;columnName&#8221;]=&gt; string(11) &#8220;removaldate&#8221; [&#8220;nullable&#8221;]=&gt; bool(true) } [&#8220;user_name&#8221;]=&gt; array(5) { [&#8220;fieldName&#8221;]=&gt; string(9) &#8220;user_name&#8221; [&#8220;type&#8221;]=&gt; string(6) &#8220;string&#8221; [&#8220;columnName&#8221;]=&gt; string(9) &#8220;user_name&#8221; [&#8220;length&#8221;]=&gt; int(255) [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;user_email&#8221;]=&gt; array(5) { [&#8220;fieldName&#8221;]=&gt; string(10) &#8220;user_email&#8221; [&#8220;type&#8221;]=&gt; string(6) &#8220;string&#8221; [&#8220;columnName&#8221;]=&gt; string(10) &#8220;user_email&#8221; [&#8220;length&#8221;]=&gt; int(255) [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;user_avatar&#8221;]=&gt; array(5) { [&#8220;fieldName&#8221;]=&gt; string(11) &#8220;user_avatar&#8221; [&#8220;type&#8221;]=&gt; string(6) &#8220;string&#8221; [&#8220;columnName&#8221;]=&gt; string(11) &#8220;user_avatar&#8221; [&#8220;length&#8221;]=&gt; int(255) [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;comment&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(7) &#8220;comment&#8221; [&#8220;type&#8221;]=&gt; string(4) &#8220;text&#8221; [&#8220;columnName&#8221;]=&gt; string(7) &#8220;comment&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;creationdate&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(12) &#8220;creationdate&#8221; [&#8220;type&#8221;]=&gt; string(8) &#8220;datetime&#8221; [&#8220;columnName&#8221;]=&gt; string(12) &#8220;creationdate&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) } [&#8220;rating&#8221;]=&gt; array(4) { [&#8220;fieldName&#8221;]=&gt; string(6) &#8220;rating&#8221; [&#8220;type&#8221;]=&gt; string(7) &#8220;integer&#8221; [&#8220;nullable&#8221;]=&gt; bool(false) [&#8220;columnName&#8221;]=&gt; string(6) &#8220;rating&#8221; } } [&#8220;fieldNames&#8221;]=&gt; array(11) { [&#8220;id&#8221;]=&gt; string(2) &#8220;id&#8221; [&#8220;parentid&#8221;]=&gt; string(8) &#8220;parentid&#8221; [&#8220;isactive&#8221;]=&gt; string(8) &#8220;isactive&#8221; [&#8220;isremoved&#8221;]=&gt; string(9) &#8220;isremoved&#8221; [&#8220;removaldate&#8221;]=&gt; string(11) &#8220;removaldate&#8221; [&#8220;user_name&#8221;]=&gt; string(9) &#8220;user_name&#8221; [&#8220;user_email&#8221;]=&gt; string(10) &#8220;user_email&#8221; [&#8220;user_avatar&#8221;]=&gt; string(11) &#8220;user_avatar&#8221; [&#8220;comment&#8221;]=&gt; string(7) &#8220;comment&#8221; [&#8220;creationdate&#8221;]=&gt; string(12) &#8220;creationdate&#8221; [&#8220;rating&#8221;]=&gt; string(6) &#8220;rating&#8221; } [&#8220;columnNames&#8221;]=&gt; array(11) { [&#8220;id&#8221;]=&gt; string(2) &#8220;id&#8221; [&#8220;parentid&#8221;]=&gt; string(8) &#8220;parentid&#8221; [&#8220;isactive&#8221;]=&gt; string(8) &#8220;isactive&#8221; [&#8220;isremoved&#8221;]=&gt; string(9) &#8220;isremoved&#8221; [&#8220;removaldate&#8221;]=&gt; string(11) &#8220;removaldate&#8221; [&#8220;user_name&#8221;]=&gt; string(9) &#8220;user_name&#8221; [&#8220;user_email&#8221;]=&gt; string(10) &#8220;user_email&#8221; [&#8220;user_avatar&#8221;]=&gt; string(11) &#8220;user_avatar&#8221; [&#8220;comment&#8221;]=&gt; string(7) &#8220;comment&#8221; [&#8220;creationdate&#8221;]=&gt; string(12) &#8220;creationdate&#8221; [&#8220;rating&#8221;]=&gt; string(6) &#8220;rating&#8221; } [&#8220;discriminatorValue&#8221;]=&gt; NULL [&#8220;discriminatorMap&#8221;]=&gt; array(0) { } [&#8220;discriminatorColumn&#8221;]=&gt; NULL [&#8220;table&#8221;]=&gt; array(1) { [&#8220;name&#8221;]=&gt; string(7) &#8220;Comment&#8221; } [&#8220;lifecycleCallbacks&#8221;]=&gt; array(0) { } [&#8220;associationMappings&#8221;]=&gt; array(0) { } [&#8220;isIdentifierComposite&#8221;]=&gt; bool(false) [&#8220;containsForeignIdentifier&#8221;]=&gt; bool(false) [&#8220;idGenerator&#8221;]=&gt; object(Doctrine\\ORM\\Id\\IdentityGenerator)#40 (1) { [&#8220;_seqName&#8221;:&#8221;Doctrine\\ORM\\Id\\IdentityGenerator&#8221;:private]=&gt; NULL } [&#8220;sequenceGeneratorDefinition&#8221;]=&gt; NULL [&#8220;tableGeneratorDefinition&#8221;]=&gt; NULL [&#8220;changeTrackingPolicy&#8221;]=&gt; int(1) [&#8220;isVersioned&#8221;]=&gt; NULL [&#8220;versionField&#8221;]=&gt; NULL [&#8220;reflClass&#8221;]=&gt; object(ReflectionClass)#41 (1) { [&#8220;name&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;isReadOnly&#8221;]=&gt; bool(false) [&#8220;reflFields&#8221;]=&gt; array(11) { [&#8220;id&#8221;]=&gt; object(ReflectionProperty)#35 (2) { [&#8220;name&#8221;]=&gt; string(2) &#8220;id&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;parentid&#8221;]=&gt; object(ReflectionProperty)#42 (2) { [&#8220;name&#8221;]=&gt; string(8) &#8220;parentid&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;isactive&#8221;]=&gt; object(ReflectionProperty)#43 (2) { [&#8220;name&#8221;]=&gt; string(8) &#8220;isactive&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;isremoved&#8221;]=&gt; object(ReflectionProperty)#44 (2) { [&#8220;name&#8221;]=&gt; string(9) &#8220;isremoved&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;removaldate&#8221;]=&gt; object(ReflectionProperty)#45 (2) { [&#8220;name&#8221;]=&gt; string(11) &#8220;removaldate&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;user_name&#8221;]=&gt; object(ReflectionProperty)#46 (2) { [&#8220;name&#8221;]=&gt; string(9) &#8220;user_name&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;user_email&#8221;]=&gt; object(ReflectionProperty)#47 (2) { [&#8220;name&#8221;]=&gt; string(10) &#8220;user_email&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;user_avatar&#8221;]=&gt; object(ReflectionProperty)#48 (2) { [&#8220;name&#8221;]=&gt; string(11) &#8220;user_avatar&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;comment&#8221;]=&gt; object(ReflectionProperty)#49 (2) { [&#8220;name&#8221;]=&gt; string(7) &#8220;comment&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;creationdate&#8221;]=&gt; object(ReflectionProperty)#50 (2) { [&#8220;name&#8221;]=&gt; string(12) &#8220;creationdate&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } [&#8220;rating&#8221;]=&gt; object(ReflectionProperty)#51 (2) { [&#8220;name&#8221;]=&gt; string(6) &#8220;rating&#8221; [&#8220;class&#8221;]=&gt; string(16) &#8220;Entities\\Comment&#8221; } } [&#8220;_prototype&#8221;:&#8221;Doctrine\\ORM\\Mapping\\ClassMetadataInfo&#8221;:private]=&gt; NULL }<\/p>\n<ol>\n<li>\n<p>You wrote<\/p>\n<pre><code>\/**\n * Entities\\Comment\n * @Entity (repositoryClass=\"Repositories\\CommentRepository\")\n *\/\nclass Comment\n{\n<\/code><\/pre>\n<p>There&#8217;s one space between <code>@Entity<\/code> and the parameters for that annotation: remove it.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:09:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Have some problems with building custom entity repository. I have the following fatal error while trying method of custom entity repository Fatal error: Uncaught exception &#8216;BadMethodCallException&#8217; with message &#8216;Undefined method &#8216;getByParentId&#8217;. The method name must start with either findBy or findOneBy!&#8217; in C:\\Users\\user\\Desktop\\projects\\interview\\application\\libraries\\Doctrine\\ORM\\EntityRepository.php:215 Stack trace: #0 C:\\Users\\user\\Desktop\\projects\\interview\\application\\controllers\\CommentController.php(58): Doctrine\\ORM\\EntityRepository-&gt;__call(&#8216;getByParentId&#8217;, Array) #1 C:\\Users\\user\\Desktop\\projects\\interview\\application\\controllers\\CommentController.php(58): Doctrine\\ORM\\EntityRepository-&gt;getByParentId(&#8216;1&#8217;) #2 [internal function]: [&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-1625","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1625","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=1625"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1625\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}