Search code examples
phpwordpressdoctrine-ormentitiesentitymanager

Doctrine 2 ORM and WordPress - Centralizing Doctrine management for multiple dependent applications


In WordPress, plugins are created and distributed as packages for other WordPress users to install.

Each of the plugins I'm creating share certain common entities in a core plugin. I'm struggling with creating and managing the EntitiyManager to use among these plugins.

I've come up with two ways to implement this and would like to hear some feedback before moving forward:

Option 1: Single, global EntityManager

// Example Plugin
    // Unique Entities
    // Add Unique Entity Paths and REQUIRED Entity Paths from Core Entities to EntityManager

// Example Plugin
    // Unique Entities
    // Add Unique Entity Paths and REQUIRED Entity Paths from Core Entities to EntityManager

// Core Plugin
    // Core Entities
    // Contains EntityManager

The problem with this version is that, although there is a way to add paths to a MetadataDriver:

$entityManager->getConfig()->getMetadataDriverImpl()->addPaths();

I am not sure if this will actually update the MetadataDriver within the config of this EntityManager. My worries is that this will only return a config that has been updated with the new paths and not update the EntityManager's config.

Is this true?

Note that there is no setConfig function so if it is, I would have to recreate the EntityManager from each plugin, defeating the purpose of a centralized EntityManager.

Option 2: Multiple EntityManagers

// Example Plugin
    // Unique Entities
    // Create EntityManager with Unique Entity path and path to Core Entities that are required for this plugin.

// Core Plugin
    // Core Entities

The problem with this implementation is that it seems inefficient. I've noticed that there is a Doctrine plugin for WordPress, although it is Doctrine 1.2.3, and it allows you to have one centralized place as I would assume is appropriate for managing applications that share resources like the Core Entities.

What are your thoughts on using Doctrine in this scenario. Is there a way to centralize the operations so that the ORM is managed through one central plugin that others depend upon?


Solution

  • I was able to figure out that:

    $entityManager->getConfiguration()->getMetadataDriverImpl()->addPaths();
    

    Does in fact add additional paths to the $entityManager object and not just to the config that is returned with getConfiguration.

    I will continue finishing up this implementation and update with my results here. Hopefully the core can become distributable as a plugin for other programmers to use.