10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13: Data Validation 222<br />

{<br />

// ...<br />

public function setTableDefinition()<br />

{<br />

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

// ...<br />

}<br />

}<br />

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

'unique' => true<br />

)<br />

);<br />

Listing<br />

13-38<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 />

# schema.yml<br />

# ...<br />

User:<br />

columns:<br />

username:<br />

type: string(255)<br />

unique: true<br />

# ....<br />

You should only use unique constraints for columns other than the primary key because<br />

they are always unique already.<br />

Regular Expression<br />

The regular expression validator is a simple way to validate column values against your own<br />

provided regular expression. In this example we will make sure the username contains only<br />

valid letters or numbers.<br />

Listing<br />

13-39<br />

// models/User.php<br />

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

{<br />

// ...<br />

public function setTableDefinition()<br />

{<br />

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

// ...<br />

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

'regexp' => '/[a-zA-Z0-9]/'<br />

)<br />

);<br />

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

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

Saved successfully!

Ooh no, something went wrong!