1. Classes declaration using Doctrine Record
- Create a new File named with the Classe name
- Declare table name
- Declare evry attribute of this classe
example :
class Address extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('addresses'); // Table name
$this->hasColumn('city', 'string',30); // An attribute
$this->hasColumn('country', 'string',20); // A second attribute
}
}
2. Make relation between Classes, this will permit you to do Object->Object2->attribute
You need tell to doctrine with which classes the current classes is linked.
In our example an Address is linked to an User. Indeed in the function named setUp, you have to declare an address hasOne User using local id and foreign key addresse.
You can declare something like this in User class to permit you to use User->Address->city
to finish don't forget to add a require_once to the classes you join to the current class.
// In Address.php
require_once('User.php');
class Address extends Doctrine_Record
{
public function setTableDefinition() { ... }
public function setUp() {
$this->hasOne('User', array('local' => 'id', 'foreign' => 'address'));
}
}
// In User.php
require_once('Address.php');
class User extends Doctrine_Record
{
public function setTableDefinition() { ... }
public function setUp() {
$this->hasOne('Address', array('local' => 'address',
'foreign' => 'id',
'onDelete' => 'CASCADE'));
}
}
3. Doctrine permit you to create tables from classes declarations in one line.
First, you need include Doctrine library in our file to permit evry operation using Doctrine.
//require the base Doctrine class
require_once('lib/Doctrine.php');
//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));
//Second, you need create the database, and create a doctrine database connexion like :
//set up a connection
Doctrine_Manager::connection('mysql://root@localhost/doctrine');
Indeed, You need include (using require_once) your Classes, and specify which Classes you wanna use to create tables.
require_once('User.php'); // Include User
require_once('Address.php'); // And Address Classes
//export the classes
Doctrine::createTablesFromArray(array('User', 'Address')); // tell to createTables which Tables you wanna create.
While script execution Doctrine will create users and adressees tables in the database doctrine
4. Using Doctrine User Classe in your applications
Like see in 3, you need include Doctrine Library to use Doctrine functions.
//require the base Doctrine class
require_once('lib/Doctrine.php');
//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));
You need include each Classe you have to use
require_once('User.php'); // Include User.php (and Address.php is auto include
// because User.php include it.
//Create a new Object User
$myuser = new User();
$myuser->name = 'John';
$myuser->login = 'john';
$myuser->password = sha1('John');
$myuser->Address->city = 'Nancy';
$myuser->Address->country = 'France';
$myuser->save(); // Will save User and his Address
Aucun commentaire:
Enregistrer un commentaire