fabiang/doctrine-dynamic

Load Doctrine mappings dynamically for your entities by configuration

v1.0.0 2021-01-13 16:00 UTC

This package is auto-updated.

Last update: 2021-10-13 17:41:47 UTC


README

Proxy Driver for Doctrine which allows you to add custom relations dynamically by configuration.

This is useful if you use foreign entities, which you can't change, but you like to add own relations between them and your entities.

Latest Stable Version License Build Status Scrutinizer Code Quality Code Coverage

Features

  • Setting all possible relations to entities:
    • OneToOne
    • ManyToOne
    • OneToMany
    • ManyToMany
  • Setting repository class

Installation

New to Composer? Read the introduction. Run the following Composer command:

$ composer require fabiang/doctrine-dynamic

Framework integration

Usage

<?php

use Fabiang\DoctrineDynamic\ConfigurationFactory;
use Fabiang\DoctrineDynamic\ProxyDriverFactory;
use Doctrine\ORM\EntityManager;

$configurationFactory = new ConfigurationFactory();
$configuration = $configurationFactory->factory([
    \Mymodule\Entity\Customer::class => [
        'options' => [
            'repository' => \Mymodule\Repository\CustomerRepository::class,
        ],
        'fields' => [
            'fieldname' => [
                'products' => [
                    'oneToMany' => [
                        [
                            'targetEntity' => \Mymodule\Entity\Customer::class,
                            'mappedBy'     => 'customer',
                        ],
                    ]
                ],
            ]
        ]
    ],
    \Mymodule\Entity\Products::class => [
        'fields' => [
            'customer' => [
                'manyToOne' => [
                    [
                        'targetEntity' => \Mymodule\Entity\Products::class,
                        'inversedBy'   => 'products',
                        'joinColumns'  => [
                            'name'                 => 'customer_id',
                            'referencedColumnName' => 'id'
                        ]
                    ],
                ]
            ],
        ]
    ],
]);

/** @var $entityManager EntityManager */
// get it from a container for example
$entityManager = $container->get(EntityManager::class);

$proxyDriverFactory = new ProxyDriverFactory();
$proxyDriverFactory->factory($entityManager, $configuration);

Development

This library is tested with PHPUnit and Behat.

Fork the project on Github and send an pull request with your changes. Make sure you didn't break anything with running the following commands:

composer install
./vendor/bin/phpunit
./vendor/bin/behat

Licence

BSD-2-Clause. See the LICENSE.md.