Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting of returned entity in Doctrine2 repository

I have CustomRepository class which extends Doctrine\ORM\EntityRepository. This repository is assosiated with many different entities. There is a method in the repository which returns an associated entity.

class CustomRepository extends \Doctrine\ORM\EntityRepository
{
    function getEntity() { ... } // returns an instance of associated entity
}

/**
 * @ORM\Entity(repositoryClass="CustomRepository")
 */
class EntityClass1 { ... }    
/**
 * @ORM\Entity(repositoryClass="CustomRepository")
 */
class EntityClass2 { ... }

$repo1 = $entityManager->getRepository('Entity1');
$entity1 = $repo1->getEntity(); // will return an instance of EntityClass1

$repo2 = $entityManager->getRepository('Entity2');
$entity2 = $repo1->getEntity(); // will return an instance of EntityClass2

I use Symfony 2 plugin, which correctly detects the class of returned by inherited methods entities as find.
Is there a way to inform the plugin that the method returns an assosiated entity?

like image 802
Vasily Avatar asked Oct 31 '22 16:10

Vasily


1 Answers

You can use something like this:

/**
 * MyEntity repository.
 *
 * @method MyEntity[] find
 */
like image 63
COil Avatar answered Nov 02 '22 09:11

COil