Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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 ObjectManager方法createQuery()似乎不存在_Php_Symfony - Fatal编程技术网

Php ObjectManager方法createQuery()似乎不存在

Php ObjectManager方法createQuery()似乎不存在,php,symfony,Php,Symfony,我有这样的类,它可以工作并从数据库返回反馈 namespace Acme\Bundle\AcmeBundle\Handler; use Doctrine\Common\Persistence\ManagerRegistry; class FeedbackHandler implements FeedbackHandlerInterface { private $em; private $entityClass; private $repository; pu

我有这样的类,它可以工作并从数据库返回反馈

namespace Acme\Bundle\AcmeBundle\Handler;

use Doctrine\Common\Persistence\ManagerRegistry;

class FeedbackHandler implements FeedbackHandlerInterface
{
    private $em;
    private $entityClass;
    private $repository;

    public function __construct(ManagerRegistry $mr, $entityClass)
    {
        $this->em = $mr->getManager();
        $this->entityClass = $entityClass;
        $this->repository = $this->em->getRepository($this->entityClass);
    }

    public function get($id)
    {
        return $this->repository->find($id);
    }

    public function all($limit = 50, $offset = 0)
    {

        $feedbacks = $this->em->createQuery("SELECT f FROM AcmeAcmeBundle:Feedback f")
            ->setFirstResult($offset)
            ->setMaxResults($limit)
            ->getResult();

        return array( "feedback" => $feedbacks );
    }

}
然而,当我将代码提交到scrutnizer-ci.com时,它会报告

上似乎不存在createQuery()方法 反对


与PhpStorm相同,它在此行显示警告。是否有解决此问题的方法?

$em
变量添加
@var
phpdoc注释

。。。或者(更好的)
@返回
ManagerRegistry
getManager()
方法

然后PHPStorm/Scrutnizer ci将知道
$em
是哪种类型,并且它有一个
createQuery()
方法

这通常是一个很好的实践,也可以在PHPStorm中实现自动完成

示例:

/** @var \Doctrine\ORM\EntityManager */
private $em;
。。。或者

class ManagerRegistry 
{

   /**
    * ...
    *
    * @return \Doctrine\ORM\EntityManager
    */ 
    public function getManager()
    { 
        // ...

“工作并从数据库返回反馈”如果工作,为什么您关心Scrutnizer-ci.com或PhpStorm(可能它们不是为Symdony服务配置的)?感谢您的回答,但是当我添加@var注释时,它显示:看起来像$mr->getManager()类型对象与属性$emMy answer的声明类型对象不兼容完全解决了最初的问题。请为您的新问题打开一个新问题。简短提示:新错误的出现显然是因为您返回了
DocumentManager
实例,而不是设置中的
EntityManger
。确保文档域与实际实例匹配。