Doctrine Resolve Target Entity

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

About Documentation Screencasts Cloud Certification Community Businesses News Store DOWNLOAD

SymfonyWorld SymfonyLive Online SymfonyLive Online SymfonyLive SymfonyLive


Online 2021 Spanish Edition German Edition Online French Online Polish
Jun 17–18, 2021 2021 2021 Edition 2021 Edition 2021
May 7, 2021 Apr 16, 2021 Apr 9, 2021 Mar 12, 2021

Home / Documentation / Doctrine / How to Define Relationships with Abstract Classes and Interfaces
Getting Started
Setup
How to Define Relationships with Abstract
Creating Pages
Routing Classes and Interfaces
Controllers
Background 5.2 version
Templates
Set up
edit this page
Configuration
Final Thoughts

Guides

Components 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
Training unnecessary items.

Certification 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.
Benchmarking the Cloud:
Background ¶
AWS vs GCP vs Azure. Read
the 2021 Cloud Report -> Suppose you have an InvoiceBundle which provides invoicing functionality and a CustomerBundle
AD S V I A CA RB O N 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.

Peruse our complete


Symfony & PHP solutions Set up ¶
catalog for your web
This article uses the following two basic entities (which are incomplete for brevity) to explain how
development needs.
to set up and use the ResolveTargetEntityListener .

A Customer entity:

// src/Entity/Customer.php
namespace App\Entity;

The life jacket for your team use App\Entity\CustomerInterface as BaseCustomer;


and your project use App\Model\InvoiceSubjectInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="customer")
*/
class Customer extends BaseCustomer implements InvoiceSubjectInterface
{
// In this example, any methods defined in the InvoiceSubjectInterface
// are already implemented in the BaseCustomer
}

An Invoice entity:

// src/Entity/Invoice.php
namespace App\Entity;
use App\Model\InvoiceSubjectInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* Represents an Invoice.
*
* @ORM\Entity
* @ORM\Table(name="invoice")
*/
class Invoice
{
/**
* @ORM\ManyToOne(targetEntity="App\Model\InvoiceSubjectInterface")
* @var InvoiceSubjectInterface
*/
protected $subject;
}

An InvoiceSubjectInterface:

// src/Model/InvoiceSubjectInterface.php
namespace App\Model;

/**
* An interface that the invoice Subject object should implement.
* In most circumstances, only a single object should implement
* this interface as the ResolveTargetEntityListener can only
* change the target to a single object.
*/
interface InvoiceSubjectInterface
{
// List any additional methods that your InvoiceBundle
// will need to access on the subject so that you can
// be sure that you have access to those methods.

public function getName(): string;


}

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

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

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.

Latest from the Symfony Blog They Help Us Make Symfony Get Involved in the Community
Symfony 5.3.0-BETA1 released A passionate group of over 600,000
April 18, 2021 Thanks Colin DeCarlo for developers from more than 120
being a Symfony countries, all committed to helping PHP
A Week of Symfony #746 (12-18 April contributor. surpass the impossible.
2021) 1 commit · 4 lines
April 18, 2021 Getting involved →

Symfony™ is a trademark of Symfony SAS. All rights reserved.

What is Symfony? Learn Symfony Screencasts Community Blog Services


Symfony at a Glance Getting Started Learn Symfony SymfonyConnect Events & Meetups Our services
Symfony Components Components Learn PHP Support A week of symfony Train developers
Case Studies Best Practices Learn JavaScript How to be Involved Case studies Manage your project quality
Symfony Releases Bundles Learn Drupal Code of Conduct Cloud Improve your project performance
Security Policy Reference Learn RESTful APIs Events & Meetups Community Host Symfony projects
Logo & Screenshots Training Projects using Symfony Conferences
Trademark & Licenses eLearning Platform Downloads Stats Diversity About
symfony1 Legacy Certification Contributors Documentation SensioLabs
Backers Living on the edge Careers
Symfony Store Releases Support
Security Advisories Cloud TOS
SymfonyInsight
Twig
Deployed on
SensioLabs

Follow Symfony

Switch to dark theme

PDFmyURL.com - convert URLs, web pages or even full websites to PDF online. Easy API for developers!

You might also like