jeudi 7 février 2008

PhpDoctrine The ORM for PHP (Little Howto)

Hi !
Today i wanna let you know about a very nice library for PHP, it's called Phpdoctrine.

The library require PDO and PHP 5.2.5. It will be release as PEAR module in sometimes.

Phpdoctrine use PDO to connect to Database. And it can connect to many DBMS (like MySQL, PgSQL, MSSQL, SQLite...).
Doctrine permit you to use an object, modify it, and save modification directly in the database with write any SQL.

For exemple :

$User = new User(1); // get the user with the ID 1 (select it from database without SQL)
echo $User->get('name'); // echo the name of the user
$User->set('name', 'His new Name');
echo $User->get('name'); // echo the NEW name of the user
$User->save(); // Save modification (UPDATE) in Database without any SQL

$User2 = new User(); // create a new Object User
$User2->save(); // Save the User, INSERT it in database without SQL

Doctrine allow you to create Php Classe (extended from Doctrine Classe), define column (id, name...) define type (integer, varchar...) and describe the Foreigns Key . Doctrine can create entire Database from this type of Classes.

Doctrine permit you to get Object from a Foreigns Key directly.

For example :

// Our User have an adresse
echo $User->Adresse->city; // display the city of the Adresse of the User (another synthax)

// Note User2 have NO address
$User2->Adresse->set('city', 'Nancy'); // set the City of the Adresse
$User2->Adresse->contry='France'; // set the Contry of the Adresse (different synthax)

$User->save(); // Save User AND his Address (and well, the association between User and Adresse)

I hope this little presentation will Convince you to try, or use it for your further projects !

You can found Doctrine documentation there
Download Doctrine 0.10 : there

ORM : Object Relational Mapper (wikipedia)
DBMS : Database management system (wikipedia)

Aucun commentaire:

Enregistrer un commentaire