benkle/doctrine-adoption-bundle
A small set of services to make doctrines inheritance mapping more useful.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require benkle/doctrine-adoption-bundle
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Benkle\DoctrineAdoptionBundle\BenkleDoctrineAdoptionBundle(),
);
// ...
}
// ...
}
/**
* Class Document
* @package AppBundle\Entity
* @Entity()
* @Table(name="documents")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"document" = "AppBundle\Entity\Document"})
*/
class Document
{
/**
* @Id()
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @Column(type="string")
*/
public $name;
}
I recommend that you...
JOINED as inheritance type, as you probably want to add children after the database was first created, and updating a large table can be expensive. Plus you'll avoid column name clashes./**
* Class TextDocument
* @package Demo\TextDocumentBundle\Entity
* @Entity()
* @Table(name="text_documents")
*/
class TextDocument extends Document
{
/**
* @Column(type="text")
*/
public $text;
}
services:
demo_text_document_bundle.text_document:
class: Demo\TextDocumentBundle\Entity\TextDocument
public: false
tags:
- name: benkle.doctrine.adoption.child
of: AppBundle\Entity\Document
discriminator: text_document
Just like Twig extensions, your entities should be declared as private services. The parameters are simple:
name: The tag name (must be benkle.doctrine.adoption.child).of: The full name of the parent class (think child of).discriminator: The value for the discriminator column.$ php bin/console cache:clear
$ php bin/console doctrine:schema:create
or
$ php bin/console doctrine:schema:update --force
How can I help you explore Laravel packages today?