Php 条令:自定义实体存储库
在构建自定义实体存储库时遇到一些问题 我在尝试自定义实体存储库的方法时出现以下致命错误Php 条令:自定义实体存储库,php,codeigniter,doctrine-orm,repository-pattern,Php,Codeigniter,Doctrine Orm,Repository Pattern,在构建自定义实体存储库时遇到一些问题 我在尝试自定义实体存储库的方法时出现以下致命错误 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\interv
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
public function viewCommentsListByParentId($parentid) {
$data = array();
$data ['comment'] = $this->em->getRepository('Entities\Comment')->getByParentId($parentid);
$this->load->view('commentsList', $data);
}
我的项目结构是
模型
映射
存储库
实体
代理
My 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'));
}
}
MyRepository类具有以下声明
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'));
}
}
调用自定义存储库方法的控制器代码
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
public function viewCommentsListByParentId($parentid) {
$data = array();
$data ['comment'] = $this->em->getRepository('Entities\Comment')->getByParentId($parentid);
$this->load->view('commentsList', $data);
}
我做错了什么???请帮忙
你写的
/**
*实体\注释
*@Entity(repositoryClass=“Repositories\CommentRepository”)
*/
课堂评论
{
在@Entity
和该注释的参数之间有一个空格:删除它。您编写的
/**
*实体\注释
*@Entity(repositoryClass=“Repositories\CommentRepository”)
*/
课堂评论
{
在
@Entity
和该注释的参数之间有一个空格:删除它。您已将条令\ORM\Mapping作为ORM导入
因此,在注释类注释中应该使用@ORM\Entity
而不是@Entity
因为现在它不加载注释[“customRepositoryClassName”]=>NULL
/**
* Entities\Comment
* @ORM\Entity (repositoryClass="Repositories\CommentRepository")
*/
class Comment
{
..
您已将条令\ORM\Mapping作为ORM导入 因此,在注释类注释中应该使用
@ORM\Entity
而不是@Entity
因为现在它不加载注释[“customRepositoryClassName”]=>NULL
/**
* Entities\Comment
* @ORM\Entity (repositoryClass="Repositories\CommentRepository")
*/
class Comment
{
..
尽管OP在评论中发布了他们的答案 当我试图调用任何条令的clear cache命令时,它抛出了一个异常
$vendor/bin/orm:clear cache:元数据无法从控制台清除APC缓存,它在Web服务器内存中共享,无法从CLI访问。
我的开发环境不使用APC,因此存储库运行正常。当我将其上载到生产服务器时,它失败,错误与OP相同
我可以通过从网页执行apc\u clear\u cache()
来解决这个问题
例如:
尽管OP在评论中发布了他们的答案
当我试图调用任何条令的clear cache命令时,它抛出了一个异常
$vendor/bin/orm:clear cache:元数据无法从控制台清除APC缓存,它在Web服务器内存中共享,无法从CLI访问。
我的开发环境不使用APC,因此存储库运行正常。当我将其上载到生产服务器时,它失败,错误与OP相同
我可以通过从网页执行apc\u clear\u cache()
来解决这个问题
例如:
我删除了。同样的结果你可以转储实体\Comment
的类元数据吗?$em->getClassMetadata('Entities\Comment');
@Grace请把它做一个要点/pastebin(先清理一下,至少它是可读的)我怎么做?我不明白你想要的元数据是什么[“customRepositoryClassName”]=>NULL
,因此很明显这是r缓存或批注未正确解析。如果按照Ocramius的建议删除空格,则批注看起来是正确的。在at第78行放置断点或放置一些调试代码以查看发生了什么。我删除了。相同的结果是否可以转储实体\Comment
的类元数据ode>$em->getClassMetadata('Entities\Comment');
@Grace请将其作为要点/粘贴箱(先清理一下,这样至少它是可读的)我该怎么做?我不明白你想要我的元数据是什么[“customRepositoryClassName”]=>NULL
,因此很明显这是第十八个缓存或批注未正确解析。如果按照Ocramius的建议删除空格,则批注看起来是正确的。请在at第78行放置断点或放置一些调试代码,以查看发生了什么情况。是否有任何元数据缓存处于活动状态?如果有,请清除它。我不能100%确定是否已缓存,但是它是类元数据的一部分。我使用apc缓存进行生产。当然,我尝试清除缓存。相同的结果您是否有任何元数据缓存处于活动状态?如果有,请清除它。我不能100%确定是否缓存了它,但它是类元数据的一部分。我使用apc缓存进行生产。当然,我尝试清除缓存。相同的结果