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

Lendable/doctrine-extensions-bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lendable Doctrine Extensions Bundle

Build Status Coverage Status Total Downloads Scrutinizer Code Quality

Licensed under the MIT License.

Provides extensions to how Doctrine is integrated with Symfony. This includes:

  • Allowing repositories to have extra constructor arguments and behind the scenes retrieval / instantiation via the container.

Requirements

  • PHP >= 7.0
  • Symfony >= 2.7

Installation

Require the bundle with Composer:

composer require lendable/doctrine-extensions-bundle

Enable the bundle:

<?php
// app/AppKernel.php

public function registerBundles() 
{
    $bundles = [
      // ...
      new Lendable\DoctrineExtensionsBundle\LendableDoctrineExtensionsBundle(),        
      // ...        
    ];
}

Usage

Repositories with dependencies

A repository with extra constructor arguments such as:

<?php
// src/App/Entity/Repository/ExampleRepository.php

namespace App\Entity\Repository;

class ExampleRepository extends EntityRepository
{
    public function __construct(
        EntityManager $entityManager, 
        ClassMetadata $classMetadata,
        string $customRawValue,
        string $customParameter, 
        CustomService $customService,
        array $customArray) 
    {
        parent::__construct($entityManager, $classMetadata);
        
        $this->customRawValue = $customRawValue;
        $this->customParameter = $customParameter;
        $this->customService = $customService;
        $this->customArray = $customArray;
    }
}

Should be configured to inform the bundle how these extra dependencies should be sourced.

lendable_doctrine_extensions:
    repositories:
        App\Entity\Repository\ExampleRepository:
            entity: App\Entity\Example
            managers: ['default', 'custom_manager']
            args:
                - 'a literal raw value'
                - '%custom_parameter%'
                - '@custom_service'
                - 
                    config: '@config_service'
                    raw_value: 'a literal raw value'

An argument can either be:

  • Raw scalar.
  • Parameter reference (%wrapped%).
  • Service reference (@prefixed).
  • An indexed/associative array of any of the above.

The repository can now be retrieved as usual via the Doctrine Registry or EntityManager.

<?php

// Via the registry...

$repository = $container->get('doctrine')->getRepository(App\Entity\Example::class);

// Via the entity manager...

$repository = $container->get('doctrine')->getManager()->getRepository(App\Entity\Example::class);