I have class like this which works and returns feedbacks from the database
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 );
}
}
however when I submit code to scrutinizer-ci.com it reports that
The method createQuery() does not seem to exist on object
same with PhpStorm, it displays warning at this line. Is there an approach to fix this issue?
Add a @var
phpdoc comment for the $em
variable ...
... or (even better) @return
to ManagerRegistry
's getManager()
method.
Then PHPStorm / scrutinizer-ci will both know of which type $em
is and that it has a createQuery()
method.
This is a good practice in general and will enable autocompletion in PHPStorm, too.
example:
/** @var \Doctrine\ORM\EntityManager */
private $em;
... or ...
class ManagerRegistry
{
/**
* ...
*
* @return \Doctrine\ORM\EntityManager
*/
public function getManager()
{
// ...