Understanding Doctrine 2 ORM and how it can and should work with WordPress

I would like to integrate Doctrine 2 ORM into WordPress for use in the plugins I’m developing. There is currently a plugin that offers Doctrine 1.2.3 ORM support in WordPress, but not v2.

The biggest problem I’m having is that I don’t understand how Doctrine 2 ORM interacts with my code; specifically, what their configuration code provides me with and where I should go from here:

Read More
// 3.1.1
require dirname( __FILE__ ) . '/lib/Doctrine/ORM/Tools/Setup.php';
$lib = dirname( __FILE__ ) . '/lib';
DoctrineORMToolsSetup::registerAutoloadDirectory($lib);

// 3.1.2
use DoctrineORMEntityManager,
    DoctrineORMConfiguration;

if($applicationMode == "development") {
    $cache = new DoctrineCommonCacheArrayCache;
} else {
    $cache = new DoctrineCommonCacheApcCache;
}

$config = new Configuration;
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver('/path/to/lib/MyProject/Entities');
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
$config->setProxyNamespace('MyProjectProxies');

if ($applicationMode == "development") {
    $config->setAutoGenerateProxyClasses(true);
} else {
    $config->setAutoGenerateProxyClasses(false);
}

$connectionOptions = array(
    'driver' => 'pdo_sqlite',
    'path' => 'database.sqlite'
);

$em = EntityManager::create($connectionOptions, $config);

I had continued reading through sections of the documentation up to section 8 and have some understanding. My questions are:

  1. Is this configuration enough to allow me to work with Doctrine 2 ORM in my plugins?

  2. Are there any other key steps I’m missing before working with Doctrine 2 ORM? The WordPress plugin seems to automatically generate all of the appropriate classes from the database. I read the documentation a few times, but I feel like I’m missing some big step… or maybe Doctrine 2 ORM is just that much different?

  3. Is the EntityManager some global variable that I can use throughout my entities?

  4. I assume I have to link everything together, @Entity in a file is not enough for Doctrine to know where the entity is. Is there somewhere in the documentation that defines this?

If anyone can provide a quick rundown of how it all works together: configuration, entities, etc. Or provide any clear cut tutorials that may already be out there, I would really appreciate it.

Related posts

Leave a Reply

1 comment

  1. IMHO you shouldn’t use Doctrine2 with WP

    1. Doctrine 2 is more appropriate solution for site with huge business logic and I believe you don’t use WP for this purposes

    2. Doctrine 2 has huge code base (~11MB) which add overhead for classloading and processing of requests

    3. Doctrine 2 use a lot of memory with default hydration mode (object)

    4. Building custom sql is much harder with Doctrine.