How to Define Relationships with Abstract Classes and Interfaces

How to Define Relationships with Abstract Classes and Interfaces

One of the goals of bundles is to create discreet bundles of functionality that do not have many (if any) dependencies, allowing you to use that functionality in other applications without including unnecessary items.

Doctrine 2.2 includes a new utility called the ResolveTargetEntityListener, that functions by intercepting certain calls inside Doctrine and rewriting targetEntity parameters in your metadata mapping at runtime. It means that in your bundle you are able to use an interface or abstract class in your mappings and expect correct mapping to a concrete entity at runtime.

This functionality allows you to define relationships between different entities without making them hard dependencies.

Background

Suppose you have an InvoiceBundle which provides invoicing functionality and a CustomerBundle that contains customer management tools. You want to keep these separated, because they can be used in other systems without each other, but for your application you want to use them together.

In this case, you have an Invoice entity with a relationship to a non-existent object, an InvoiceSubjectInterface. The goal is to get the ResolveTargetEntityListener to replace any mention of the interface with a real object that implements that interface.

Set up

This article uses the following two basic entities (which are incomplete for brevity) to explain how to set up and use the ResolveTargetEntityListener.

A Customer entity:

  1. // src/Entity/Customer.php
  2. namespace App\Entity;
  3. use App\Entity\CustomerInterface as BaseCustomer;
  4. use App\Model\InvoiceSubjectInterface;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7. * @ORM\Entity
  8. * @ORM\Table(name="customer")
  9. */
  10. class Customer extends BaseCustomer implements InvoiceSubjectInterface
  11. {
  12. // In this example, any methods defined in the InvoiceSubjectInterface
  13. // are already implemented in the BaseCustomer
  14. }

An Invoice entity:

  1. // src/Entity/Invoice.php
  2. namespace App\Entity;
  3. use App\Model\InvoiceSubjectInterface;
  4. use Doctrine\ORM\Mapping as ORM;
  5. /**
  6. * Represents an Invoice.
  7. *
  8. * @ORM\Entity
  9. * @ORM\Table(name="invoice")
  10. */
  11. class Invoice
  12. {
  13. /**
  14. * @ORM\ManyToOne(targetEntity="App\Model\InvoiceSubjectInterface")
  15. * @var InvoiceSubjectInterface
  16. */
  17. protected $subject;
  18. }

An InvoiceSubjectInterface:

  1. // src/Model/InvoiceSubjectInterface.php
  2. namespace App\Model;
  3. /**
  4. * An interface that the invoice Subject object should implement.
  5. * In most circumstances, only a single object should implement
  6. * this interface as the ResolveTargetEntityListener can only
  7. * change the target to a single object.
  8. */
  9. interface InvoiceSubjectInterface
  10. {
  11. // List any additional methods that your InvoiceBundle
  12. // will need to access on the subject so that you can
  13. // be sure that you have access to those methods.
  14. public function getName(): string;
  15. }

Next, you need to configure the listener, which tells the DoctrineBundle about the replacement:

  • YAML

    1. # config/packages/doctrine.yaml
    2. doctrine:
    3. # ...
    4. orm:
    5. # ...
    6. resolve_target_entities:
    7. App\Model\InvoiceSubjectInterface: App\Entity\Customer
  • XML

    1. <!-- config/packages/doctrine.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/doctrine
    9. https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
    10. <doctrine:config>
    11. <doctrine:orm>
    12. <!-- ... -->
    13. <doctrine:resolve-target-entity interface="App\Model\InvoiceSubjectInterface">App\Entity\Customer</doctrine:resolve-target-entity>
    14. </doctrine:orm>
    15. </doctrine:config>
    16. </container>
  • PHP

    1. // config/packages/doctrine.php
    2. use App\Entity\Customer;
    3. use App\Model\InvoiceSubjectInterface;
    4. $container->loadFromExtension('doctrine', [
    5. 'orm' => [
    6. // ...
    7. 'resolve_target_entities' => [
    8. InvoiceSubjectInterface::class => Customer::class,
    9. ],
    10. ],
    11. ]);

Final Thoughts

With the ResolveTargetEntityListener, you are able to decouple your bundles, keeping them usable by themselves, but still being able to define relationships between different objects. By using this method, your bundles will end up being easier to maintain independently.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.