Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Declare Doctrine repository as a Symfony service

License

Notifications You must be signed in to change notification settings

davidkmenta/RepoServiceBundle

Repository files navigation

RepoServiceBundle

FOR EXPERIMENTAL USE ONLY!

The RepoServiceBundle provides the ability to declare a Doctrine repositories as a Symfony services.

Requirements

  • Symfony >= 3
  • Doctrine >= 2.5
  • PHP >= 5.6

Installation

Require the bundle with the composer:

composer require davidkmenta/repo-service-bundle "dev-master"

Enable the bundle in the kernel:

<?php
// app/AppKernel.php
 
public function registerBundles()
{
    $bundles = [
        // ...
        new DavidKmenta\RepoServiceBundle\RepoServiceBundle(),
        // ...
    ];
}

Documentation

  • Note: This bundle overrides the doctrine.orm.entity_manager.class option and setups an own entity manager.
  • First of all, you have to create or update your repository class. A repository class has to extend the class DavidKmenta\RepoServiceBundle\Repository\EntityRepository.
  • Now you have to implement a method getMappedEntityName which tells to the EntityManager what entity is managed by this repository. The best practise is, return a fully qualified class name:
<?php

namespace AcmeBundle\Repository;

use AcmeBundle\Entity\CustomEntity;
use DavidKmenta\RepoServiceBundle\Repository\EntityRepository;
use Psr\Log\LoggerInterface;

class CustomRepository extends EntityRepository
{
    public function __construct(LoggerInterface $logger)
    {
        // ...
    }
    
    public function getMappedEntityName()
    {
        return CustomEntity::class;
    }
}
  • Last step is about defining the repository as a service. It's as simple as any other definition:
acme.repository.custom:
    class: AcmeBundle\Repository\CustomRepository
    arguments: ["@logger"]
    tags:
        - { name: doctrine.repository }

That's it! Yes, the trick is in the tag doctrine.repository :-) and the logger is injected as you're expecting.

TODOs and known issues

  • Instead of using the method getMappedEntityName, declare an entity name through a class annotation.
  • A custom entity manager isn't supported.

Disclaimer

Using this bundle is on own risk.

License

MIT

Contributing

Any contribution is welcomed :-)

About

Declare Doctrine repository as a Symfony service

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages