5

I got this problem I have a method that is repetitive in all the Repositories, for example this method.

function getAllOrderedBy($column) {

    $qb = $this->createQueryBuilder('ac')
            ->select('ac')
            ->orderBy('ac.' . $column);

    return $qb->getQuery()->getResult();
}

I want to extract it in another superclass, OrderedRepository for example and use it as the base class for all the other repositories.

Now the problem is how to do that ?

I tried to instantiate EntityRepository in the constructor of the OrderedRepository, something like this, but also instantiating there all the internal objects, needed for other stuff, but it didn't really worked, and I felt it is the wrong path to follow.

function __construct() {

  parent::__construct();
  $this->blabla_option = "instantiated";
}

Could you please give an example of correct extending of EntityRepository so than this extended class could serve as a base class for other repositories ?

P.S. I'm a begginer in PHP so please excuse me if I hurt your feelings with my unawareness.

Patrick
  • 879
  • 2
  • 11
  • 29
Monomachus
  • 1,448
  • 2
  • 13
  • 22

1 Answers1

13

This is more a Doctrine2 thing.

Assuming you are using annotations for your doctrine mapping, you have to declare which repository class you are using in the Entity:

/**
 * @ORM\Entity(repositoryClass="Fully\Qualified\Namespace\To\MyRepository")
 */
class MyEntity { }

as explained here: http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes .

Then, you can code this custom MyRepository class, using standard class inheritance.

You could imagine something like that:

class OrderedRepository extends EntityRepository 
{
    //  some extra methods...
}

class MyRepository extends OrderedRespository {}

Finally, if you want to override the __constructor of your repository, you have to initailize the parent constructor with the same arguments:

public function __construct($em, Mapping\ClassMetadata $class)
{
    parent::__construct($em, $class);
    // some extra stuff
}
Florian Klein
  • 8,692
  • 1
  • 32
  • 42
  • 1
    Catchable Fatal Error: Argument 2 passed to OrderedRepository::__construct() must be an instance of Metadata\ClassMetadata, instance of Doctrine\ORM\Mapping\ClassMetadata given, called in ...vendor\doctrine\lib\Doctrine\ORM\EntityManager.php on line 578 and defined in OrderedRepository.php line 18 line 18 : public function __construct($em, ClassMetadata $class) – Monomachus Feb 12 '12 at 13:02
  • You must add the corresponding **use** statements on top of your class, or type-hint the fully qualified namespaces. example: public function __construct($em, Doctrine\ORM\Mapping\ClassMetadata $class) – Florian Klein Feb 12 '12 at 13:05
  • Yeah, thanks, it worked, although now I need to implement constructor in each Repository, which confronts the initial goal :( – Monomachus Feb 12 '12 at 13:23
  • I wouldn't advise you to override constructor. Don't you have another solution ? What are you planning to do in constructor? – Florian Klein Feb 12 '12 at 13:29
  • In this concrete case I wanted to set parent $entityAlias that would be used in OrderedRepository for creating query. But I think there could be a more reasonable cause for it, probably at this point I just haven't reach it. – Monomachus Feb 12 '12 at 13:58