39

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.

Features:

  • 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:

application/models
application/models/fixtures/schema
application/models/proxies
application/models/repositories

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.

/models/fixtures/schema/models.Product.yml

models\Product:
  type: entity
  table: products
  repositoryClass: models\repositories\ProductRepository
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 255
  oneToMany:
    serial:
      targetEntity: Serial
      mappedBy: product

/models/fixtures/schema/models.Serial.yml

models\Serial:
  type: entity
  table: serials
  repositoryClass: models\repositories\SerialRepository
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    serial:
      type: string
      length: 255
  manyToOne:
    product:
      targetEntity: Product
      inversedBy: serial
      joinColumn:
        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:

/modules/doctrine2/init.php
/modules/doctrine2/bin/cli-config.php

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');
		Doctrine::em()->persist($Product);
		Doctrine::em()->flush();
 
		$response = "Created Product with ID " . $Product->getId();
 
		if ( Kohana::VERSION >= 3.1 )
			$this->response->body($response);
		else
			$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