【问题标题】:Doctrine: custom entity repository学说:自定义实体存储库
【发布时间】:2013-03-01 11:31:17
【问题描述】:

在构建自定义实体存储库时遇到一些问题。

我在尝试自定义实体存储库的方法时遇到以下致命错误

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

我的项目结构是 楷模 映射 存储库 实体 代理

我的学说.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
{

我的 Repository 类有以下声明

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 
}

【问题讨论】:

  • 您是否有任何元数据缓存处于活动状态?如果是这样,清除它。我不能 100% 确定这是否被缓存,但它是类元数据的一部分。
  • 我使用 apc 缓存进行生产。当然,我试图清除缓存。同样的结果

标签: php codeigniter doctrine-orm repository-pattern


【解决方案1】:

您已将 Doctrine\ORM\Mapping 导入为 ORM。

所以你应该在 Comment 类注解中使用 @ORM\Entity 而不是 @Entity

因为现在它不加载注解["customRepositoryClassName"]=> NULL

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

【讨论】:

    【解决方案2】:

    你写的

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

    @Entity 和该注释的参数之间有一个空格:删除它。

    【讨论】:

    • 我删除了。同样的结果
    • 你能转储Entities\Comment 的类元数据吗? $em->getClassMetadata('Entities\Comment');
    • @Grace 请制作一个 gist/pastebin 代替(先清理它,这样至少它是可读的)
    • 我该怎么做?我不明白你想要我什么
    • 在你的元数据中是["customRepositoryClassName"]=> NULL,所以很明显是八级缓存或者注解解析不正确。如果您按照 Ocramius 的建议删除空格,您的注释看起来是正确的。在第 78 行的AnnotationDriver 中放置一个断点或一些调试代码,看看会发生什么。
    【解决方案3】:

    尽管 OP 在 cmets 中发布了他们的答案。

    当我尝试调用任何学说的 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() 来解决此问题。之后自定义存储库加载正常。

    例如: http://mydoamin.com/clear-cache.php

    <?php
    
    apc_clear_cache();
    

    在 CLI 中运行它只会清除 CLI 缓存而不是 Web 服务器的缓存。 http://php.net/manual/en/function.apc-clear-cache.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多