2013-03-01 80 views
0

在構建自定義實體存儲庫時遇到一些問題。原則:自定義實體存儲庫

我有以下的致命錯誤,而試圖自定義實體庫的方法

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->__call('getByParentId', Array) #1 C:\Users\user\Desktop\projects\interview\application\controllers\CommentController.php(58): Doctrine\ORM\EntityRepository->getByParentId('1') #2 [internal function]: CommentController->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 

我的項目結構是 車型 映射 庫 實體 代理

我doctrine.php具有存儲庫管理器,例如這

// load the repositories 
$repositoryClassLoader = new \Doctrine\Common\ClassLoader('Repositories', APPPATH.'models'); 
$repositoryClassLoader->register(); 

我的模型類有以下聲明

namespace Entities; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Entities\Comment 
* @Entity (repositoryClass="Repositories\CommentRepository") 
*/ 
class Comment 
{ 

我的倉庫類具有以下聲明

namespace Repositories; 
use Doctrine\ORM\EntityRepository; 
use Entities; 

class CommentRepository extends EntityRepository { 
    public function getByParentId($parentid) { 
     return parent::findBy(array('parentid' => $parentid), array('creationdate' => 'DESC')); 
    } 
} 

控制器代碼中調用自定義庫的方法

public function viewCommentsListByParentId($parentid) { 
     $data = array(); 
     $data ['comment'] = $this->em->getRepository('Entities\Comment')->getByParentId($parentid); 

     $this->load->view('commentsList', $data); 
    } 

我在做什麼錯誤???請幫忙。


object(Doctrine\ORM\Mapping\ClassMetadata)#34 (32) { 
    ["name"]=> string(16) "Entities\Comment" 
    ["namespace"]=> string(8) "Entities" 
    ["rootEntityName"]=> string(16) "Entities\Comment" 
    ["customRepositoryClassName"]=> NULL 
    ["isMappedSuperclass"]=> bool(false) 
    ["parentClasses"]=> array(0) { } 
    ["subClasses"]=> array(0) { } 
    ["namedQueries"]=> array(0) { } 
    ["identifier"]=> array(1) { 
     [0]=> string(2) "id" 
    } 
    ["inheritanceType"]=> int(1) 
    ["generatorType"]=> int(4) 
    ["fieldMappings"]=> array(11) { 
     ["id"]=> array(4) { 
      ["fieldName"]=> string(2) "id" 
      ["type"]=> string(7) "integer" 
      ["id"]=> bool(true) 
      ["columnName"]=> string(2) "id" 
     } 
     ["parentid"]=> array(4) { 
      ["fieldName"]=> string(8) "parentid" 
      ["type"]=> string(7) "integer" 
      ["columnName"]=> string(8) "parentid" 
      ["nullable"]=> bool(false) 
     } 
     ["isactive"]=> array(4) { 
      ["fieldName"]=> string(8) "isactive" 
      ["type"]=> string(7) "integer" 
      ["columnName"]=> string(8) "isactive" 
      ["nullable"]=> bool(false) 
     } 
     ["isremoved"]=> array(4) { 
      ["fieldName"]=> string(9) "isremoved" 
      ["type"]=> string(7) "integer" 
      ["columnName"]=> string(9) "isremoved" 
      ["nullable"]=> bool(false) 
     } 
     ["removaldate"]=> array(4) { 
      ["fieldName"]=> string(11) "removaldate" 
      ["type"]=> string(8) "datetime" 
      ["columnName"]=> string(11) "removaldate" 
      ["nullable"]=> bool(true) 
     } 
     ["user_name"]=> array(5) { 
      ["fieldName"]=> string(9) "user_name" 
      ["type"]=> string(6) "string" 
      ["columnName"]=> string(9) "user_name" 
      ["length"]=> int(255) 
      ["nullable"]=> bool(false) 
     } 
     ["user_email"]=> array(5) { 
      ["fieldName"]=> string(10) "user_email" 
      ["type"]=> string(6) "string" 
      ["columnName"]=> string(10) "user_email" 
      ["length"]=> int(255) 
      ["nullable"]=> bool(false) 
     } 
     ["user_avatar"]=> array(5) { 
      ["fieldName"]=> string(11) "user_avatar" 
      ["type"]=> string(6) "string" 
      ["columnName"]=> string(11) "user_avatar" 
      ["length"]=> int(255) 
      ["nullable"]=> bool(false) 
     } 
     ["comment"]=> array(4) { 
      ["fieldName"]=> string(7) "comment" 
      ["type"]=> string(4) "text" 
      ["columnName"]=> string(7) "comment" 
      ["nullable"]=> bool(false) 
     } 
     ["creationdate"]=> array(4) { 
      ["fieldName"]=> string(12) "creationdate" 
      ["type"]=> string(8) "datetime" 
      ["columnName"]=> string(12) "creationdate" 
      ["nullable"]=> bool(false) 
     } 
     ["rating"]=> array(4) { 
      ["fieldName"]=> string(6) "rating" 
      ["type"]=> string(7) "integer" 
      ["nullable"]=> bool(false) 
      ["columnName"]=> string(6) "rating" 
     } 
    } 
    ["fieldNames"]=> array(11) { 
     ["id"]=> string(2) "id" 
     ["parentid"]=> string(8) "parentid" 
     ["isactive"]=> string(8) "isactive" 
     ["isremoved"]=> string(9) "isremoved" 
     ["removaldate"]=> string(11) "removaldate" 
     ["user_name"]=> string(9) "user_name" 
     ["user_email"]=> string(10) "user_email" 
     ["user_avatar"]=> string(11) "user_avatar" 
     ["comment"]=> string(7) "comment" 
     ["creationdate"]=> string(12) "creationdate" 
     ["rating"]=> string(6) "rating" 
    } 
    ["columnNames"]=> array(11) { 
     ["id"]=> string(2) "id" 
     ["parentid"]=> string(8) "parentid" 
     ["isactive"]=> string(8) "isactive" 
     ["isremoved"]=> string(9) "isremoved" 
     ["removaldate"]=> string(11) "removaldate" 
     ["user_name"]=> string(9) "user_name" 
     ["user_email"]=> string(10) "user_email" 
     ["user_avatar"]=> string(11) "user_avatar" 
     ["comment"]=> string(7) "comment" 
     ["creationdate"]=> string(12) "creationdate" 
     ["rating"]=> string(6) "rating" 
    } 
    ["discriminatorValue"]=> NULL 
    ["discriminatorMap"]=> array(0) { } 
    ["discriminatorColumn"]=> NULL 
    ["table"]=> array(1) { 
     ["name"]=> string(7) "Comment" 
    } 
    ["lifecycleCallbacks"]=> array(0) { } 
    ["associationMappings"]=> array(0) { } 
    ["isIdentifierComposite"]=> bool(false) 
    ["containsForeignIdentifier"]=> bool(false) 
    ["idGenerator"]=> object(Doctrine\ORM\Id\IdentityGenerator)#40 (1) { 
     ["_seqName":"Doctrine\ORM\Id\IdentityGenerator":private]=> NULL 
    } 
    ["sequenceGeneratorDefinition"]=> NULL 
    ["tableGeneratorDefinition"]=> NULL 
    ["changeTrackingPolicy"]=> int(1) 
    ["isVersioned"]=> NULL 
    ["versionField"]=> NULL 
    ["reflClass"]=> object(ReflectionClass)#41 (1) { 
     ["name"]=> string(16) "Entities\Comment" 
    } 
    ["isReadOnly"]=> bool(false) 
    ["reflFields"]=> array(11) { 
     ["id"]=> object(ReflectionProperty)#35 (2) { 
      ["name"]=> string(2) "id" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["parentid"]=> object(ReflectionProperty)#42 (2) { 
      ["name"]=> string(8) "parentid" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["isactive"]=> object(ReflectionProperty)#43 (2) { 
      ["name"]=> string(8) "isactive" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["isremoved"]=> object(ReflectionProperty)#44 (2) { 
      ["name"]=> string(9) "isremoved" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["removaldate"]=> object(ReflectionProperty)#45 (2) { 
      ["name"]=> string(11) "removaldate" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["user_name"]=> object(ReflectionProperty)#46 (2) { 
      ["name"]=> string(9) "user_name" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["user_email"]=> object(ReflectionProperty)#47 (2) { 
      ["name"]=> string(10) "user_email" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["user_avatar"]=> object(ReflectionProperty)#48 (2) { 
      ["name"]=> string(11) "user_avatar" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["comment"]=> object(ReflectionProperty)#49 (2) { 
      ["name"]=> string(7) "comment" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["creationdate"]=> object(ReflectionProperty)#50 (2) { 
      ["name"]=> string(12) "creationdate" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
     ["rating"]=> object(ReflectionProperty)#51 (2) { 
      ["name"]=> string(6) "rating" 
      ["class"]=> string(16) "Entities\Comment" 
     } 
    } 
    ["_prototype":"Doctrine\ORM\Mapping\ClassMetadataInfo":private]=> NULL 
} 
+0

您是否有任何元數據緩存處於活動狀態?如果是這樣,清除它。我不是100%確定這是否緩存,但它是類元數據的一部分。 – 2013-03-01 12:53:00

+0

我使用apc緩存進行生產。當然,我試圖清除緩存。相同的結果 – 2013-03-01 13:03:18

回答

6

您已將Doctrine \ ORM \ Mapping導入爲ORM。

所以你應該使用@ORM\Entity而不是@Entity評論類的註釋。

因爲現在它不會加載註釋["customRepositoryClassName"]=> NULL

/** 
    * Entities\Comment 
    * @ORM\Entity (repositoryClass="Repositories\CommentRepository") 
    */ 
class Comment 
{ 
.. 
+1

這應該是被接受的答案。 – Craimasjien 2014-07-23 11:36:27

0

你寫

/** 
* Entities\Comment 
* @Entity (repositoryClass="Repositories\CommentRepository") 
*/ 
class Comment 
{ 

有和該標註的參數@Entity之間有一個空格:刪除它。

+0

我刪除了。相同的結果 – 2013-03-01 12:27:05

+0

您可以轉儲「Entities \ Comment」的類元數據嗎? '$ em-> getClassMetadata('Entities \ Comment');' – Ocramius 2013-03-01 12:29:01

+0

我發佈了我的答案和類元數據 – 2013-03-01 12:36:09

0

儘管OP發佈他們的意見答覆。

當我試圖調用任何doctrine的clear-cache命令時,它拋出一個異常。

$ vendor/bin/doctrine orm:clear-cache:metadata Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.

我的開發環境不使用APC,因此存儲庫運行良好。當我將其上傳到生產服務器時,它與OP發生同樣的錯誤失敗。

我能夠通過從網頁執行apc_clear_cache()來解決此問題。之後,自定義存儲庫加載正常。

EG: http://mydoamin.com/clear-cache.php

<?php 

apc_clear_cache(); 

在CLI運行它只會清除緩存CLI而不是web服務器的緩存。 http://php.net/manual/en/function.apc-clear-cache.php