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 13: Data Validation 225<br />

// models/User.php<br />

Listing<br />

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

}<br />

}<br />

$this->hasColumn('age', 'integer', 3, array(<br />

'unsigned' => true<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 />

# schema.yml<br />

Listing<br />

13-47<br />

# ...<br />

User:<br />

columns:<br />

# ...<br />

age:<br />

type: integer(3)<br />

unsigned: true<br />

# ...<br />

Now if I try and save a User with a negative age the validation will fail:<br />

// test.php<br />

Listing<br />

13-48<br />

// ...<br />

$user = new User();<br />

$user->username = 'jwage';<br />

$user->age = '-100';<br />

if ( ! $user->isValid()) {<br />

echo 'User is invalid because -100 is signed';<br />

}<br />

US State<br />

The us state validator checks that the giv<strong>en</strong> string is a valid US state code.<br />

// models/State.php<br />

Listing<br />

13-49<br />

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

{<br />

public function setTableDefinition()<br />

{<br />

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

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

Saved successfully!

Ooh no, something went wrong!