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 226<br />

}<br />

}<br />

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

$this->hasColumn('code', 'string', 2, array(<br />

'usstate' => true<br />

)<br />

);<br />

Listing<br />

13-50<br />

Listing<br />

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

State:<br />

columns:<br />

name: string(255)<br />

code:<br />

type: string(2)<br />

usstate: true<br />

Now if I try and save a State with an invalid state code th<strong>en</strong> the validation will fail.<br />

$state = new State();<br />

$state->name = 'T<strong>en</strong>nessee';<br />

$state->code = 'ZZ';<br />

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

echo 'State is invalid because "ZZ" is not a valid state code';<br />

}<br />

Conclusion<br />

If we want <strong>Doctrine</strong> to validate our data before being persisted to the database, now we have<br />

the knowledge on how to do it. We can use the validators that are provided with the <strong>Doctrine</strong><br />

core to perform common validations of our data.<br />

The next chapter (page 231) is an important one as we will discuss a great feature of <strong>Doctrine</strong>,<br />

Inheritance (page 231)! Inheritance is a great way accomplish complex functionality with<br />

minimal code. After we discuss inheritance we will move on to a custom strategy that<br />

provides ev<strong>en</strong> better functionality than inheritance, called Behaviors (page 239).<br />

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

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

Saved successfully!

Ooh no, something went wrong!