10

I have class ModelsRepository:

class ModelsRepository extends EntityRepository
{}

And service

container_data:
 class:        ProjectName\MyBundle\Common\Container
 arguments:    [@service_container]

I want get access from ModelsRepository to service container_data. I can't transmit service from controller used constructor.

Do you know how to do it?

8 Answers 8

14

IMHO, this shouldn't be needed since you may easily break rules like SRP and Law of Demeter

But if you really need it, here's a way to do this:

First, we define a base "ContainerAwareRepository" class which has a call "setContainer"

services.yml

services:
    # This is the base class for any repository which need to access container
    acme_bundle.repository.container_aware:
        class: AcmeBundle\Repository\ContainerAwareRepository
        abstract: true
        calls:
            - [ setContainer, [ @service_container ] ]

The ContainerAwareRepository may looks like this

AcmeBundle\Repository\ContainerAwareRepository.php

abstract class ContainerAwareRepository extends EntityRepository
{
    protected $container;

    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;
    }
}

Then, we can define our Model Repository.
We use here, the doctrine's getRepository method in order to construct our repository

services.yml

services:
    acme_bundle.models.repository:
        class: AcmeBundle\Repository\ModelsRepository
        factory_service: doctrine.orm.entity_manager
        factory_method:  getRepository
        arguments:
            - "AcmeBundle:Models"
        parent:
            acme_bundle.repository.container_aware

And then, just define the class

AcmeBundle\Repository\ModelsRepository.php

class ModelsRepository extends ContainerAwareRepository
{
    public function findFoo()
    {
        $this->container->get('fooservice');
    }
}

In order to use the repository, you absolutely need to call it from the service first.

$container->get('acme_bundle.models.repository')->findFoo(); // No errors
$em->getRepository('AcmeBundle:Models')->findFoo(); // No errors

But if you directly do

$em->getRepository('AcmeBundle:Models')->findFoo(); // Fatal error, container is undefined
3
  • If my repository has a method searchUserByNameLike and returns an array of Users where the name is like ... and this method has a rule where only admins can see certain user details (like email) (affecting the DQL). Where exactly would I place this function and how do inject a Rules service that's dependent on a User service (the id of the user)? I don't think this is the responsibility of the controller as that just gets the UserRepository (User entity, not User class containing the current user id). This because a search like this could be needed in several controls and control funcions
    – HMR
    Commented Jul 22, 2014 at 16:00
  • @HMR That's a different question. I'd create a "wrapper" class which will take UserRepository as an argument. Then you'd have a method in your wrapper like findUsersForUserByNameLike($name, User $user) which will first check permissions of the given user, build the query and then process the results. For the "rules" condition, I'd add an array of Rule interface with methods check(User $user) and build(UserRepository $users) With this, you want to traverse this array and check which rule matches the given user.
    – Touki
    Commented Jul 22, 2014 at 20:11
  • Thanks!! I used the same explanation the use getService on my Service Layer. ;) Commented Jul 29, 2015 at 3:07
7

I tried some versions. Problem was solved follows

ModelRepository:

class ModelRepository extends EntityRepository
{
    private $container;

    function __construct($container, $em) {
        $class = new ClassMetadata('ProjectName\MyBundle\Entity\ModelEntity');
        $this->container = $container;

        parent::__construct($em, $class);
    }
}

security.yml:

providers:
    default:
        id: model_auth

services.yml

model_auth:
    class: ProjectName\MyBundle\Repository\ModelRepository
    argument

As a result I got repository with ability use container - as required. But this realization can be used only in critical cases, because she has limitations for Repository. Thx 4all.

7

You should never pass container to the repository, just as you should never let entities handle heavy logic. Repositories have only one purpose - retrieving data from the database. Nothing more (read: http://docs.doctrine-project.org/en/2.0.x/reference/working-with-objects.html).

If you need anything more complex than that, you should probably create a separate (container aware if you wish) service for that.

4

I would suggest using a factory service:

http://symfony.com/doc/current/components/dependency_injection/factories.html

//Repository
class ModelsRepositoryFactory
{
    public static function getRepository($entityManager,$entityName,$fooservice)
    {
        $em     = $entityManager;
        $meta   = $em->getClassMetadata($entityName);

        $repository = new ModelsRepository($em, $meta, $fooservice);
        return $repository;
    }
}

//service
AcmeBundle.ModelsRepository:
        class: Doctrine\ORM\EntityRepository
        factory: [AcmeBundle\Repositories\ModelsRepositoryFactory,getRepository]
        arguments:
            - @doctrine.orm.entity_manager
            - AcmeBundle\Entity\Models
            - @fooservice  
4

Are you sure that is a good idea to access service from repo?

Repositories are designed for custom SQL where, in case of doctrine, doctrine can help you with find(),findOne(),findBy(), [...] "magic" methods.

Take into account to inject your service where you use your repo and, if you need some parameters, pass it directly to repo's method.

4

I strongly agree that this should only be done when absolutely necessary. Though there is a quite simpler approach possible now (tested with Symfony 2.8).

  1. Implement in your repository "ContainerAwareInterface"
  2. Use the "ContainerAwareTrait"
  3. adjust the services.yml

RepositoryClass:

namespace AcmeBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use AcmeBundle\Entity\User;

class UserRepository extends EntityRepository implements ContainerAwareInterface
{

    use ContainerAwareTrait;

    public function findUserBySomething($param)
    {
        $service = $this->container->get('my.other.service');
    }

}

services.yml:

acme_bundle.repository.user:
    lazy: true
    class: AcmeBundle\Repository\UserRepository
    factory: ['@doctrine.orm.entity_manager', getRepository]
    arguments:
        - "AcmeBundle:Entity/User"
    calls:
        - method: setContainer
          arguments:
            - '@service_container'
0

the easiest way is to inject the service into repository constructor.

class ModelsRepository extends EntityRepository
{
  private $your_service;

  public function __construct(ProjectName\MyBundle\Common\Container $service) {
    $this->your_service = $service;
  } 
}
3
  • But you don't have direct access to repository constructor so you have to pass service as argument in DIC.
    – S3Mi
    Commented Oct 19, 2012 at 10:09
  • I written: "I can't transmit service from controller used constructor."
    – Alastor
    Commented Oct 19, 2012 at 10:11
  • 3
    from my experience if you can not do something it is your architecture fault, not the framework. You're just doings things in the wrong place. Commented Oct 26, 2012 at 15:31
0

Extending Laurynas Mališauskas answer, to pass service to a constructor make your repository a service too and pass it with arguments:

models.repository:
      class: ModelsRepository
      arguments: ['@service_you_want_to_pass']
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.