Better Doctrine 2 Module for Kohana 3

Posted (Updated ) in Database, PHP

Update Feb 22 2012: 1.0.8 is out. Now works with and requires Doctrine 2.2. Thanks to Markus for his patch.
Update Nov 05 2011: Updated to version 1.0.7. Now works with Kohana 3.2 and supports schema files located in modules.
Update Jun 22 2011: Updated to version 1.0.6. Now works with Kohana 3 and 3.1 and any Kohana directory structure. The repo has also been moved to GitHub.
Update Feb 21 2011: Updated to version 1.0.5. Fixed some errors with the SQL logger when passing objects to DQL – see changelog for more details.

Since releasing my original Doctrine 2 module for Kohana 3, I’ve done a bit of reshuffling of folders and added some additional features from my Doctrine 1.2 module. Due to the extent of modifications, I decided to put up a new post with some added information on how to use the new module.


  • Doctrine 2 integration (obviously)
  • Auth and Session drivers
  • /doctrine controller to load your schema files and generate the DB tables
  • Works in Kohana 3.0 and 3.1

For the impatient ones out there, here’s the download link:
Doctrine 2 Module for Kohana 3


As always, install the module (remembering to add it to your bootstrap file) and download the latest copy of Doctrine 2 from here, placing into the /modules/doctrine2/classes/vendor/doctrine folder. It should be noted that my module is tailored to the tarball download and not the SVN or Github ones which have a slightly different folder structure.

The Structure

All entities/fixtures/proxies are located in:


The /doctrine Controller

This module includes a /doctrine controller which accesses the Doctrine 2 CLI tool. Due to the dodginess of the CLI tool included in the tarball version (at least back when I was first writing my module), I wrote my own and included it in the module.

Please make sure the doctrine file in this folder is executable

chmod +x /modules/doctrine2/bin/doctrine

As noted above, I’ve included a /doctrine controller. This controller allows you to perform many of the CLI’s functions including validating the schema, generating your entitiy files (models) and creating/updating your database tables to match your schema files. As a security precaution, by default this controller is only accessible from localhost – add any IPs you need into the controller file.

Unlike the D1.2 module’s controller, this one will not delete your data. It will update entity files adding any missing fields or functions and it will update the database fields instead of needing to delete/recreate. For the DB table section, I’ve also added a button to show the SQL statements the tool will attempt to perform on the database. You should always use this before clicking the Create Tables button!

The Schema

I prefer using YAML files to generate my entities. They’re placed in /models/fixtures/schema and must be named in a specific way. Each entity requires it’s own YAML file. For your convenience, here are 2 example YAML files and their respective names.


  type: entity
  table: products
  repositoryClass: models\repositories\ProductRepository
      type: integer
        strategy: AUTO
      type: string
      length: 255
      targetEntity: Serial
      mappedBy: product


  type: entity
  table: serials
  repositoryClass: models\repositories\SerialRepository
      type: integer
        strategy: AUTO
      type: string
      length: 255
      targetEntity: Product
      inversedBy: serial
        name: product_id
        referencedColumnName: id

D2 currently doesn’t have great documentation on YAML files so you may wish to switch to one of the other annotation methods such as XML. To do so you’ll need to update the following 2 files:


As of version 1.0.7 of my module (released Nov 05 2011) you can now also place schema files in the /models/fixtures/schema directory of individual modules. Entities/Proxies/Repos from these schema files will still be generated into application/models.

Example Usage:

I was asked for some example usage. So here it is:

<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Welcome extends Controller {
	public function action_index()
		$this->response->body('hello, world!');
	public function action_test()
		$Product = new \models\Product();
		$Product->setName('New Product');
		$response = "Created Product with ID " . $Product->getId();
		if ( Kohana::VERSION >= 3.1 )
			$this->request->response = $response;
} // End Welcome

Much Wootage!

Your module is now set up. Congratulations 🙂

For those who missed it, here’s the complete module download link again – be sure to read the installation instructions included in the download:
Doctrine 2 Module for Kohana 3