Doctrine in a PHP programming
Programming PHP | Posted: July 6th, 2008 | ozzTags: database, Doctrine, MySQL, php, programming, scripts example
Home page: www.doctrine-project.org. Current version: 1.0.2
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.
For more visit http://www.doctrine-project.org/documentation
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
[source='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;
?>
[/source]
Example of Users.php
[source='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
));
}
}
?>
[/source]






Print
Send
Leave a Reply