Doctrine

Doctrine is the main module for managing models in an Enbici application. Enbici works with Doctrine 2.1 2.0 and 1.2 but support for 1.2 is limited since the validations provided by Enbici are currently tied to the 2.1, 2.0 branch of Doctrine. But Doctrine 1.2 provides its own validation system you can use.

What is Doctrine?

Object relational mapper (ORM) for PHP that sits on top of a powerful database abstraction layer (DBAL). One of its key features is the option to write database queries in a proprietary object oriented SQL dialect called Doctrine Query Language (DQL), inspired by Hibernates HQL. This provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication. (Doctrine Website)

Examples

Here is a sample of a user model in doctrine.

//in models/user.php
class User extends EnbiciEntity{
	protected $name;
	protected $email;
	
	public function getName(){
		return $this->nombre;
	}
	
	public function setName($name){
		$this->name = $name
	}
	
	public function getEmail(){
		return $this->email;
	}
	
	public function setEmail($email){
		$this->email = $email;
	}
}

This is a sample of the user class. This represents the user table in the Database with all of its properties, Doctrine requires that you specify the get and set methods for the model in order to be able to use it.

This is the user yml mapping Doctrine will use for this class

	//in db/User.yml
	User:
	  type: entity
	  id:
	    id:
	      type: integer
	      generator:
	        strategy: AUTO
	  fields:
	    name: 
	      type: string
	    email:
	      type: string

Enbici currently works with Doctrine yml mappings, but a way to support xml and php mapping will be made available.

As you can see the user class is a subclass of EnbiciEntity which is part of the EnbiciEntity module, and by telling doctrine about the database mapping in the yml file, doctrine can easily retrieve and save the model.

	//in application/controllers/UserController.php
	public function createAction(){
		$user = new User;
		$postvars = $this->filter($_POST);
		$user->name = $postvars["name"];
		$user->email = $postvars["email"];
		$user->save();
	}

Since user is a subclass of EnbiciEntity you can easily just call the save method on it in order to store the recently created user. In order to retreive the user we can just do the follwing:

	public function showAction($name){
		$users = User::findBy(array('name' => $name));
		$this->view->assign('users', $users);
	}

Here we demonstrated only a subset of the functionality provided by Doctrine, in order to learn more about it as well as to how to set up associations like one-to-many, many-to-many, please visit the Doctrine Website.

blog comments powered by Disqus