benkle / doctrine-adoption-bundle
A small set of services to make doctrines inheritance mapping more useful
Installs: 25
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >= 5.6
- benkle/doctrine-adoption: 1.0.*
- symfony/config: >=2.8
- symfony/dependency-injection: >=2.8
- symfony/http-kernel: >=2.8
Requires (Dev)
- phpunit/phpunit: ^5.4
This package is not auto-updated.
Last update: 2025-01-18 21:58:32 UTC
README
A small set of services to make doctrines inheritance mapping more useful.
Installation
Step 1: Download the Bundle
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.
Step 2: Enable the Bundle
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(), ); // ... } // ... }
Usage
Step 1: Define a parent entity
/** * 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...
- ...use
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. - ...at least declare the discriminator map with the parent as only entry. Mostly for clarity's sake.
Step 2: Define a child
/** * Class TextDocument * @package Demo\TextDocumentBundle\Entity * @Entity() * @Table(name="text_documents") */ class TextDocument extends Document { /** * @Column(type="text") */ public $text; }
Step 3: Create a service definition, so your child can be adopted
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 bebenkle.doctrine.adoption.child
).of
: The full name of the parent class (think child of).discriminator
: The value for the discriminator column.
Step 4: Clear the cache
$ php bin/console cache:clear
Step 5: Create or update db
$ php bin/console doctrine:schema:create
or
$ php bin/console doctrine:schema:update --force