Integrating Doctrine Into WordPress

Posted (Updated ) in Database, PHP

WordPress is great on its own but if you want to do alot of database work with it you’ll quickly realize how frustratingly limited the wpdb class is. In this tutorial I’ll show how to integrate Doctrine into WordPress through the use of a plugin and make your developing life so much easier.

This tutorial uses the latest stable versions of Doctrine and WordPress at the time of writing (1.2.1 and 2.9.2 respectively). I’ll skip over the boring bits like downloading and setting up your WordPress Installation and downloading the latest Doctrine – if you’re reading this, chances are you know how to or have done that already.

Let’s get started.

1. in /wp-content/plugins create a doctrine direectory
2. Extract your Doctrine zip file into this directory such that the COPYRIGHT, CHANGELOG etc files are located in /wp-content/plugins/doctrine/doctrine
3. Now that we have our doctrine files we need to link it to WordPress. In /wp-content/plugins/doctrinedoc create a file doctrine.php with the following contents:

* @package Doctrine DBMS
* @author Flynsarmy
* @version 1.0.0
Plugin Name: Doctrine DBMS Integration
Plugin URI: http://www.flynsarmy.com/
Description: This plugin enables Doctrine DBMS support in WordPress to ease development.
Author: Flynsarmy
Version: 1.0.0
Author URI: http://www.flynsarmy.com/
	// load Doctrine library
	require_once dirname(__FILE__).'/doctrine/lib/Doctrine.php';
	// this will allow Doctrine to load Model classes automatically
	spl_autoload_register(array('Doctrine', 'autoload'));
	$dsn = 'mysql://' . DB_USER .
		':' . DB_PASSWORD .
		'@' . DB_HOST .
		'/' . DB_NAME;
		Doctrine_Manager::connection($dsn, 'default');
	// telling Doctrine where our models are located
	Doctrine::loadModels( dirname(__FILE__).'/models' );
	// load our shortcodes
	foreach( glob(dirname(__FILE__) . '/shortcodes/*.php') as $sc_file )
		require_once( $sc_file );
	// this will allow us to use "mutators"
	// this sets all table columns to notnull and unsigned (for ints) by default
		array('notnull' => true, 'unsigned' => true));
	// set the default primary key to be named 'id', integer, 4 bytes
		array('name' => 'id', 'type' => 'integer', 'length' => 4));

You should now have a folder structure that looks like this:

Plugin Folder Structure 1
Plugin Folder Structure 1

4. You may have noticed in doctrine.php there are two links to folders we don’t have yet. The first to models on line 21 and the second to shortcodes on line 34. Models is where the table definitions go. Shortcodes is where any Doctrine-related shortcodes you might want to add go into. Create both these folders. Your folder structure should now look like the following:

Plugin Folder Structure 2
Plugin Folder Structure 2

Note – I used my glob() method to auto-load shortcode files. You can get more information on the method here.

At this point Doctrine you should be able to to go the plugin page in your administration panel and activate the Doctrine DBMS Integration plugin. You should now have a working Doctrine installation. Enjoy 🙂