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 7: Defining Models 54<br />

Column Aliases<br />

<strong>Doctrine</strong> offers a way of setting column aliases. This can be very useful wh<strong>en</strong> you want to<br />

keep the application logic separate from the database logic. For example if you want to<br />

change the name of the database field all you need to change at your application is the<br />

column definition.<br />

Listing<br />

7-1<br />

// models/Book.php<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

$this->hasColumn('bookTitle as title', 'string');<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 />

Listing<br />

7-2<br />

---<br />

# schema.yml<br />

# ...<br />

Book:<br />

columns:<br />

bookTitle:<br />

name: bookTitle as title<br />

type: string<br />

Now the column in the database is named bookTitle but you can access the property on your<br />

objects using title.<br />

Listing<br />

7-3<br />

// test.php<br />

// ...<br />

$book = new Book();<br />

$book->title = 'Some book';<br />

$book->save();<br />

Default values<br />

<strong>Doctrine</strong> supports default values for all data types. Wh<strong>en</strong> default value is attached to a record<br />

column this means two things. First this value is attached to every newly created Record and<br />

wh<strong>en</strong> <strong>Doctrine</strong> creates your database tables it includes the default value in the create table<br />

statem<strong>en</strong>t.<br />

Listing<br />

7-4<br />

// models/g<strong>en</strong>erated/BaseUser.php<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

$this->hasColumn('username', 'string', 255, array('default' =><br />

'default username'));<br />

// ...<br />

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

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

Saved successfully!

Ooh no, something went wrong!