Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm Doesn't Recognize Doctrine Entity Classes

PhpStorm doesn't seem to recognize any of my Doctrine entity classes or their methods.

$comment = $em->getRepository('MyBundle:Comment')->findOneBy(array('id' => $id));

$comment->getId(); 
/* PHPSTORM THROWS WARNING: Method getId() not found in subject class */

The error goes away, only when I explicitly comment it - which really clutters my controller.

/* @var \MyBundle\Entity\Comment $comment */
$comment = $em->getRepository('MyBundle:Comment')->findOneBy(array('id' => $id));

Is there a way to document this for PhpStorm in my Entity Class?

I'm using the Symfony2 plugin with PhpStorm 8. Thanks!

like image 541
Acyra Avatar asked Dec 10 '14 18:12

Acyra


3 Answers

I have the same problem with the Symfony2 Plugin, this is maybe not a nice solution but it works

/** @var EntityManager $em */
$em = $this->doctrine->getManager();
like image 86
felipep Avatar answered Nov 10 '22 00:11

felipep


your issue should be fixed, now. there was a problem on multiple getRepository implementations of proxy classes in cache folder. just update to >= 0.11.81

like image 21
Daniel Espendiller Avatar answered Nov 09 '22 23:11

Daniel Espendiller


Now, I prefer declaring repositories as services so you don't have those typehint problems:

services:
    MyBundle\Entity\CommentRepository:
        class: MyBundle\Entity\CommentRepository
        public: true
        factory: ['@doctrine', getRepository]
        arguments:
            - MyBundle\Entity\Comment

Then in your controller :

use MyBundle\Entity\CommentRepository;

$comment = $this->get(CommentRepository::class)->findOneBy(['id' => $id]);
like image 37
COil Avatar answered Nov 10 '22 01:11

COil