Top

PHP5 Databases ORM (Objects Relation Models) - Doctrine

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Posted the by admin

This is a short introduction to a great databases abstraction in PHP5. It can handle the mapping of database fields to php objects (ORM). It is called “Doctrine”.

Instead of Propel, Doctrine support two modes :

1) Pre-generation of the model.

2) Runtime in-memory creation of the model.



The second mode is really interresting for debuging.

1: Describe your Object (you can use PHP object or Yaml language)

<?php
 
class Email extends Doctrine_Record
{
    public function setTableDefinition()
    {
        // setting custom table name:
        $this->setTableName('emails');
 
        $this->hasColumn('address',         // name of the column
                         'string',          // column type
                         '200',             // column length
                         array('notblank' => true)
                         );
    }
}
 
?>

This means that our “email” table has a column type “String” called “address” not empty. For more detailled informations, please take a look at the Doctrine documentation.

2: Now, use it !

<?php
$user = new Email();
$user->address= 'toto@toto.fr';
// Saves the reccord
$user->save();
?>

If you want to know more about Doctrine (and I am sure that you want to), please following the bottom links.

Community Area - You can add comments here




Bottom