Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 定制存储库建立后,如何访问条令定义的方法?_Php_Symfony_Zend Framework2_Doctrine - Fatal编程技术网

Php 定制存储库建立后,如何访问条令定义的方法?

Php 定制存储库建立后,如何访问条令定义的方法?,php,symfony,zend-framework2,doctrine,Php,Symfony,Zend Framework2,Doctrine,我正在开发一个网站使用ZF2和条令。我面临的问题是,我在代码中使用了预定义的对象方法,如findAll()、findOneBy()、findBy()等。对于一些自定义操作,我为我的一个实体准备了一个自定义存储库。现在我无法访问预定义的方法。我已经使用findAll()方法编写了代码。但在构建存储库之后,我不能简单地访问findAll()方法。如何既访问自定义方法,又访问条令定义方法 例如: 我使用findOneBy()的方式如下: $udata = $this->em()->getR

我正在开发一个网站使用ZF2和条令。我面临的问题是,我在代码中使用了预定义的对象方法,如findAll()、findOneBy()、findBy()等。对于一些自定义操作,我为我的一个实体准备了一个自定义存储库。现在我无法访问预定义的方法。我已经使用findAll()方法编写了代码。但在构建存储库之后,我不能简单地访问findAll()方法。如何既访问自定义方法,又访问条令定义方法

例如:

我使用findOneBy()的方式如下:

$udata = $this->em()->getRepository('Application\Entity\Usermain')->findOneBy(array('userEmail' => 'subh.laha@gmail.com'));
现在,我已经准备好UserMain存储库,如下所示:

namespace Application\Entity\Repositories;

use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Persistence\ObjectRepository;

class UsermainRepository extends EntityRepository
{   
    protected $sl;

    public function __construct($sl){

        $this->sl = $sl;    
    }

    public function customFind($arr)
    {
        $qb = $this->sl->createQueryBuilder();

        $whereStr = '';

        if(count($arr)){

            foreach($arr as $kvarr=>$varr){

                $whereStr .= "u.$kvarr = '".$varr."'";  
            }   
        }

        $qry = $qb->select('u')
                   ->from('Application\Entity\Usermain','u')
                   ->where($whereStr)
                   ->getQuery()
                   ->getResult(); 

        return $qry;
    }
}
现在我可以访问

$udata = $this->em()->getRepository('Application\Entity\Usermain')->customFind(array('userEmail' => 'subh.laha@gmail.com'));
但不是

$udata = $this->em()->getRepository('Application\Entity\Usermain')->findOneBy(array('userEmail' => 'subh.laha@gmail.com'));

为什么??我已经使用条令定义的方法编写了代码。我现在能做什么?

我相信您会遇到此错误,因为您已重写了存储库的构造函数方法,但未调用父构造函数,因此未正确设置所需参数。

您的方法中存在几个问题

  • 我认为尝试在实体存储库中访问服务定位器本身是个坏主意。您不应该需要存储库级别的服务容器
  • 第二个细节是,在扩展任何类时,您需要签出、阅读并尊重父类的签名。在您的情况下,您正在覆盖父级的
    \uu构造
    。调用
    parent::\uu construct()
    似乎是一个解决方案,但事实并非如此。您很快就会意识到,您还需要一个定制的存储库工厂来向构造函数传递额外的参数,同时保持当前的功能。不可能
  • 这比其他更重要:您认为
    $This->sl->createQueryBuilder()
    返回查询生成器实例。从理论上讲,这似乎是可行的,但
    $this->sl
    不是服务定位器,服务定位器对查询生成器一无所知,它只是传递给构造函数的
    EntityManager
    实例
试试这个:

<?php
namespace Application\Entity\Repositories;

use Doctrine\ORM\EntityRepository;

class UsermainRepository extends EntityRepository
{

    public function customFind($arr)
    {
        // Just pass an alias for your entity
        $qb = $this->createQueryBuilder('u');

        $whereStr = '';

        if (count($arr)) {
            foreach ($arr as $kvarr => $varr) {
                $whereStr .= "u.$kvarr = '".$varr."'";
            }
        }

        return $qb->where($whereStr)
                  ->getQuery()
                  ->getResult();
    }
}
现在,在控制器(或服务)级别,您可以测试:

$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$repo = $em->getRepository('Application\Entity\Usermain');
// repo is a UsermainRepository instance
// this should work:
$udata = $repo->customFind(array('userEmail' => 'subh.laha@gmail.com'));
// this should also work
$udata = $repo->findOneBy(array('userEmail' => 'subh.laha@gmail.com'));

我强烈建议在潜入深海之前仔细阅读文档的一节

我认为您的代码不正确,您可以使用下面的查询,而不是编写复杂的自定义对象

$query = $this->getEntityManager()->createQueryBuilder()
                    ->select('U.id,U.name')  
                    ->from('Application\Entity\StudentClass', 'U')
                    ->where('U.pkStudentClass = :pkStudentClass')
                    ->setParameter('pkStudentClass', 1)
                    ->setMaxResults(20); 
                    ->orderBy('id', 'DESC') 
                    ->getQuery();

 $result = $query->getScalarResult();

扩展
EntityRepository
默认情况下允许您访问预定义的方法。当您尝试执行
findOneBy()
时,您会得到什么错误?致命错误:在第行的/var/www/html/new\u mpt\u hk/mpt\u hk/vendor/doctrine/orm/lib/doctrine/orm/EntityRepository.php中对非对象调用成员函数getUnitOfWork()194@SubhasisLaha你这里有很多问题。为什么更改了类的方法签名?您至少需要有
\u构造(条令\ORM\EntityManager$em,条令\ORM\Mapping\ClassMetadata$class)
。还有,为什么要注入
$sl
?我假设它代表
ServiceLocator
?不需要它,
qb=$this->sl->createQueryBuilder()错了。我无法得到你的答案。如何调用父构造函数以及在何处调用?你能给我举个例子吗?我陷入了困境。Plz help.@subbasislaha这个答案是正确的,并且是相当基本的OOP,您需要将
父::\u构造($entityManager,$classMetadata)
添加到
\u构造
方法中。+1@subbasislaha如果这个答案对您有帮助,
$query = $this->getEntityManager()->createQueryBuilder()
                    ->select('U.id,U.name')  
                    ->from('Application\Entity\StudentClass', 'U')
                    ->where('U.pkStudentClass = :pkStudentClass')
                    ->setParameter('pkStudentClass', 1)
                    ->setMaxResults(20); 
                    ->orderBy('id', 'DESC') 
                    ->getQuery();

 $result = $query->getScalarResult();