Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating portable Bundles with extendable entities in Symfony2

I want to create some Symfony2 bundles which are reusable accross different projects, but where the entities also can be easily extended if required.

An example could be a reusable UserBundle, which contains a User entity with all the ORM mappings defined. In my application however, I might want to extend this entity and add extra columns, associations or override some of the parent's mappings.

The closest solution I could find are Doctrine2's mapped superclasses, but then I'd lose the plug-and-playness of my reusable bundle, I'd always have to extend the mapped superclass in my application even if I don't wish to modify the mappings.

The other documented inheritance schemes require modifying the parent's mappings, and then my UserBundle wouldn't be portable anymore accross projects.

Is there a way to define a fully-working entity in one bundle, and still extend that in another bundle?

like image 460
Gerry Avatar asked Mar 21 '12 08:03

Gerry


1 Answers

For future reference, this can be solved using target entity resolution.

You can find extra information in Symfony docs.

The steps are pretty straighforward:

  1. Create an interface in your bundle for the User entity

    namespace Acme/UserBundle/Model;
    interface UserInterface
    {
        // public functions expected for entity User
    }
    
  2. Make your base User entity implement the interface

    namespace Acme/UserBundle/Entity;
    /**
     * @ORM\Entity
     */
    class User implements UserInterface
    {
        // implement public functions
    }
    
  3. Create relationships as usual, but using the interface

    namespace Acme/InvoiceBundle/Entity;
    /**
     * @ORM\Entity
     */
    class Invoice
    {
        /**
         * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Model\UserInterface")
         */
        protected $user;
    }
    
  4. Configure the listener by adding the following to config.yml

    doctrine:
        # ....
        orm:
            # ....
            resolve_target_entities:
                Acme\UserBundle\Model\UserInterface: Acme\UserBundle\Entity\User
    

If you want to customize User entity for your current application

  1. Extend from the User class or implement UserInterface

    namespace Acme/WebBundle/Entity;
    use Acme/UserBundle/Entity/User as BaseUser;
    /**
     * @ORM\Entity
     */
    class User extends BaseUser
    {
        // Add new fields and functions
    }
    
  2. Configure the listener accordingly

    doctrine:
        # ....
        orm:
            # ....
            resolve_target_entities:
                Acme\UserBundle\Model\UserInterface: Acme\WebBundle\Entity\User
    
like image 136
xPheRe Avatar answered Nov 11 '22 03:11

xPheRe