Home - Web Developer Blog

doctrine2-logo

Doctrine 2 Classes – how to create them and all you need to know

Doctrine 2 is a powerful ORM alternative to Zend_Db. Although it is admittedly a little harder to get your head around, the benefits are huge when it all clicks together. This article will teach how to create a Doctrine 2 class to store and get data in a database.

A Doctrine 2 class is just a normal PHP class, but with certain requirements such as:

  1. Class variables must be defined for each field to be saved to the database. This needs to be either private or protected and should never be prefixed with an underscore. These class variables are the field names you will use in any queries you may make.
  2. The only exception for class variables are foreign keys, they do not need to be defined in this way. Instead a variable for the related object should be defined (Link to foreign Key Post).
  3. An array class variable can be used for objects that have many of another object (if they are on the inverse side of a foreign relationship).
  4. The primary key should not be settable. When the object is saved Doctrine 2 will automatically set it. As a result of this automation, primary keys should not be a composite key of other data fields.
  5. Classes can be standalone or can be extended from parent classes. Fields that are stored in the database can be included from the parent class, but must be protected rather than private.
  6. Default values are not specified by the database schema, but can be by using default values in the class definition or constructor.
  7. The constructor is ran when the class is instantiated, but when the it is retrieved from the database it won’t be!

Here is an example class, which defines a primary key, several text fields and two fields for foreign relationships; author (many Pages have one Author) and comments (a Page has many Comments)

class Page
{
	private $id;
	private $title;
	private $content;
	private $author;
	private $comments = array();

	public function getId()
	{
		return $this->id;
	}

	public function getTitle()
	{
		return $this->title;
	}

	public function setTitle($title)
	{
		$this->title = $title;
	}

	public function getContent()
	{
		return $this->content;
	}

	public function setContent($content)
	{
		$this->content = $content;
	}

	public function getAuthor()
	{
		return $this->content;
	}

	public function setAuthor($content)
	{
		$this->content = $content;
	}

	public function getComments()
	{
		return $this->comments;
	}
}

Once you have returned an object of the page class from the database, you can easily access any of the data fields at any point in time by using a simple line of code such as:

 echo $page->getComments(); 

As you can probably guess this will echo the comments obtained from the database for the relevant page. It is as easy as that!

Another requirement for any Model in Doctrine 2 is that it needs to be mapped. You can learn how to map your Doctrine 2 classes correctly in our post Doctrine 2 Mapping – How To Create Then And All You Need To Know.

Net Tuts Code Canyon Get Noticed - $7.99 .Com or .CO from GoDaddy.com!