Free Quote
Blog

Doctrine in a PHP programming

Home page: www.doctrine-project.org. Current version: 1.0.2

Doctrine is a PHP ORM for PHP 5.2.3+ that sits on the top of a powerful PHP. One of its key features is the ability to optionally write database queries in an object oriented SQL-dialect called DQL inspired by Hibernates HQL.

This provides web developers with a powerful alternative to SQL that maintains a maximum flexibility without requiring needless code duplication.Doctrine requires PHP 5.2.3+. It doesn’t require any external libraries.

For the database function call abstraction Doctrine uses PDO which comes bundled with the PHP official release that you get from www.php.net.

Doctrine features:

  • Supports: MySQL, SQLite, PostgreSQL, Oracle, Mssql, Firebird and Informix.
  • Supports result caching with Memcache, APC or SQLite.
  • Supports import/export with YAML schemas, SQL scripts or existing database.
  • Has DQL (Doctrine Query Language) it helping users in complex object retrieval.
  • Supports migration functionality. For making a migration process easy.
  • Has integrated searching functionality.

Example:

This example illustrates the way of extracting data from the database table.

File structure:

Models/Users.php - user model
Doctrine/ - Doctrine library files
Doctrine.php – Doctrine main file
index.php

Example of index.php

// include Dotrine main file
require_once 'Doctrine.php';

// register Doctrine library file loader
spl_autoload_register(array('Doctrine','autoload'));

// connect to database
Doctrine_Manager::connection('mysql://user:password@localhost/database');

// set path to models
Doctrine::loadModels('models');

// create simple query on DQL
$users = Doctrine_Query::create()->from('Users');

// print result
foreach ($users as $user)
echo $user->email;

Example of Users.php

/** * User model definition. */
class Users extends Doctrine_Record {
/** * Define model metadata. */
public function setTableDefinition() {
$this->setTableName('users');
$this->hasColumn(
'id',
'integer',
4,
array(
'type' => 'integer',
'length' => 11,
'primary' => true,
'autoincrement' => true
));

$this->hasColumn(
'email',
'string',
255,
array(
'type' => 'string',
'length' => 255,
'default' => '',
'notnull' => true
));
}
}
Leave a Comment
Your email will not be published