10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15: Inheritance 234<br />

Listing<br />

15-12<br />

CREATE TABLE text_item (id BIGINT AUTO_INCREMENT,<br />

topic VARCHAR(100),<br />

PRIMARY KEY(id)) ENGINE = INNODB<br />

CREATE TABLE comm<strong>en</strong>t (id BIGINT AUTO_INCREMENT,<br />

topic VARCHAR(100),<br />

cont<strong>en</strong>t TEXT,<br />

PRIMARY KEY(id)) ENGINE = INNODB<br />

In concrete inheritance you don't necessarily have to define additional columns, but in order<br />

to make <strong>Doctrine</strong> create separate tables for each class you'll have to make iterative<br />

setTableDefinition() calls.<br />

In the following example we have three database tables called <strong>en</strong>tity, user and group.<br />

Users and groups are both <strong>en</strong>tities. The only thing we have to do is write 3 classes<br />

(Entity, Group and User) and make iterative setTableDefinition() method calls.<br />

Listing<br />

15-13<br />

// models/Entity.php<br />

Listing<br />

15-14<br />

class Entity ext<strong>en</strong>ds <strong>Doctrine</strong>_Record<br />

{<br />

public function setTableDefinition()<br />

{<br />

$this->hasColumn('name', 'string', 30);<br />

$this->hasColumn('username', 'string', 20);<br />

$this->hasColumn('password', 'string', 16);<br />

$this->hasColumn('created', 'integer', 11);<br />

}<br />

}<br />

// models/User.php<br />

class User ext<strong>en</strong>ds Entity<br />

{<br />

public function setTableDefinition()<br />

{<br />

// the following method call is needed in<br />

// concrete inheritance<br />

par<strong>en</strong>t::setTableDefinition();<br />

}<br />

}<br />

// models/Group.php<br />

class Group ext<strong>en</strong>ds Entity<br />

{<br />

public function setTableDefinition()<br />

{<br />

// the following method call is needed in<br />

// concrete inheritance<br />

par<strong>en</strong>t::setTableDefinition();<br />

}<br />

}<br />

Here is the same example in YAML format. You can read more about YAML in the YAML<br />

Schema Files (page 195) chapter:<br />

---<br />

Entity:<br />

columns:<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!