Category Archives: Doctrine

Simple Nested Sets in Doctrine 2

Unlike Doctrine 1 with it’s NestedSet behaviour, there is no nested set functionality in the core of Doctrine 2. There are a few extensions available that offer nested set support:

I tried all of these extensions, but none of them felt simple or lightweight enough for my application. What I wanted to do was have a Category entity which could have a tree of sub-categories, e.g: (more…)

Doctrine 2: Resolving “unknown database type enum requested”

I came across this recently while I was developing a module for PyroCMS. Some of the PyroCMS tables contain ENUM columns, which Doctrine doesn’t support. You would think that this wouldn’t be an issue since these tables are not mapped, but apparently when Doctrine builds the schema it includes all tables in the database – even if they are not mapped. This has been reported as an issue, but the Doctrine team has given it a low priority.

The symptom? When using the SchemaTool to create, update, or drop the schema; an exception is thrown:

Fatal error: Uncaught exception 'Doctrine\DBAL\DBALException' with message 'Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.'

Thankfully, the fix is very easy. There is even a Doctrine Cookbook article about it. All you have to do is register the ENUM type as a Doctrine varchar (string):

/** @var $em \Doctrine\ORM\EntityManager */
$platform = $em->getConnection()->getDatabasePlatform();
$platform->registerDoctrineTypeMapping('enum', 'string');

This fix can be applied to any unsupported data type, for example SET (which is also used in PyroCMS):

$platform->registerDoctrineTypeMapping('set', 'string');

Doctrine 2.1 Released

Doctrine 2.1 has been released, bringing many major changes to the ORM – some of which are not backwards-compatible with Doctrine 2.0. (more…)

CodeIgniter 2/Doctrine 2 Installation

If you want a quick way of getting Doctrine 2 working with CodeIgniter 2, you can download a pre-configured installation from my GitHub repository. There are currently three branches available:

  • master – the latest stable versions of CodeIgniter and Doctrine.
  • develop – the latest development versions of CodeIgniter and Doctrine. Not recommended for production use.
  • doctrine-2.1 - the latest stable version of CodeIgniter, and the latest stable 2.1.X version of Doctrine. This branch is provided as a way of keeping Doctrine 2.1 applications up to date without breaking backwards compatibility.

For more information, read my post on integrating Doctrine 2 with CodeIgniter 2

Useful Doctrine 2 Console Commands

Doctrine 2′s console is really powerful when you know how to use it. You can generate entity classes and their method stubs, reverse-engineer a database, validate your entity schemas, and much more. In this post, I’m going to cover some of the Doctrine console’s more useful commands and explain how you can use them to reduce development time. For a full overview of the Doctrine 2 console, read the Doctrine Tools documentation. (more…)

eAccelerator and Doctrine 2

A word of warning: eAccelerator does not play well with Doctrine 2. This came to my attention today after I installed eAccelerator so that I could measure the performance gains (if any). As it turns out, one of eAccelerator’s “features” is to remove Docblocks from PHP scripts – probably to reduce compile times. Suddenly my application was throwing exceptions with the message “Class X is not a valid entity or mapped super class”.

(more…)

Custom Doctrine 2 Console Commands

This post assumes you have set up Doctrine 2 with CodeIgniter 2.

Load Data from Fixtures

I could not find a command to load data from fixtures, so I made a very basic command that recursively executes native SQL. If you want to load data from YAML files, you will need to search elsewhere for a YAML interpreter or even a way to convert YAML to SQL.

(more…)

Integrating Doctrine 2 with CodeIgniter 2

If you’re looking for a quick way to get Doctrine 2 running with CodeIgniter 2, you might want to download my CodeIgniter 2/Doctrine 2 package

Overview

CodeIgniter is a great PHP framework. The codebase is clean, the documentation is fantastic, and it’s regularly updated. Doctrine is a good ORM for the same reasons: it’s very well-written, has extensive documentation, and is actively developed. A combination of these two systems makes it easy to build database-oriented web applications quicker than ever before.

To get started, download CodeIgniter 2 and Doctrine 2 – make sure you use the ‘Download Archive’ link when downloading Doctrine.

(more…)