Chapter 13: Data Validation 226 } } $this->hasColumn('name', 'string', 255); $this->hasColumn('code', 'string', 2, array( 'usstate' => true ) ); Listing 13-50 Listing 13-51 Here is the same example in YAML format. You can read more about YAML in the YAML Schema Files (page 195) chapter: --- # schema.yml # ... State: columns: name: string(255) code: type: string(2) usstate: true Now if I try and save a State with an invalid state code then the validation will fail. $state = new State(); $state->name = 'Tennessee'; $state->code = 'ZZ'; if ( ! $state->isValid()) { echo 'State is invalid because "ZZ" is not a valid state code'; } Conclusion If we want Doctrine to validate our data before being persisted to the database, now we have the knowledge on how to do it. We can use the validators that are provided with the Doctrine core to perform common validations of our data. The next chapter (page 231) is an important one as we will discuss a great feature of Doctrine, Inheritance (page 231)! Inheritance is a great way accomplish complex functionality with minimal code. After we discuss inheritance we will move on to a custom strategy that provides even better functionality than inheritance, called Behaviors (page 239). ----------------- Brought to you by
Chapter 14: Data Hydrators 227 Chapter 14 Data Hydrators Doctrine has a concept of data hydrators for transforming your Doctrine_Query instances to a set of PHP data for the user to take advantage of. The most obvious way to hydrate the data is to put it into your object graph and return models/class instances. Sometimes though you want to hydrate the data to an array, use no hydration or return a single scalar value. This chapter aims to document and demonstrate the different hydration types and even how to write your own new hydration types. Core Hydration Methods Doctrine offers a few core hydration methods to help you with the most common hydration needs. Record The first type is HYDRATE_RECORD and is the default hydration type. It will take the data from your queries and hydrate it into your object graph. With this type this type of thing is possible. $q = Doctrine_Core::getTable('User') ->createQuery('u') ->leftJoin('u.Email e') ->where('u.username = ?', 'jwage'); Listing 14-1 $user = $q->fetchOne(array(), Doctrine_Core::HYDRATE_RECORD); echo $user->Email->email; The data for the above query was retrieved with one query and was hydrated into the object graph by the record hydrator. This makes it much easier to work with data since you are dealing with records and not result sets from the database which can be difficult to work with. Array The array hydration type is represented by the HYDRATE_ARRAY constant. It is identical to the above record hydration except that instead of hydrating the data into the object graph using PHP objects it uses PHP arrays. The benefit of using arrays instead of objects is that they are much faster and the hydration does not take as long. ----------------- Brought to you by
Doctrine ORM for PHP Guide to Doctr
Table of Contents iii Examples ....
Table of Contents v Sample Queries
Table of Contents vii Component Que
Table of Contents ix Inserting a No
Table of Contents xi Generating Mig
Chapter 1: Introduction 13 Chapter
Chapter 1: Introduction 15 layer de
Chapter 1: Introduction 17 Further
Chapter 2: Getting Started 19 Insta
Chapter 2: Getting Started 21 Downl
Chapter 2: Getting Started 23 In th
Chapter 3: Introduction to Connecti
Chapter 3: Introduction to Connecti
Chapter 3: Introduction to Connecti
Chapter 4: Configuration 31 $table-
Chapter 4: Configuration 33 backtic
Chapter 4: Configuration 35 class M
Chapter 4: Configuration 37 // boot
Chapter 5: Connections 39 Chapter 5
Chapter 5: Connections 41 $conn = D
Chapter 5: Connections 43 This is p
Chapter 6: Introduction to Models 4
Chapter 6: Introduction to Models 4
Chapter 6: Introduction to Models 4
Chapter 6: Introduction to Models 5
Chapter 7: Defining Models 53 Chapt
Chapter 7: Defining Models 55 } } /
Chapter 7: Defining Models 57 class
Chapter 7: Defining Models 59 Strin
Chapter 7: Defining Models 61 class
Chapter 7: Defining Models 63 class
Chapter 7: Defining Models 65 $this
Chapter 7: Defining Models 67 onUpd
Chapter 7: Defining Models 69 // te
Chapter 7: Defining Models 71 First
Chapter 7: Defining Models 73 } } $
Chapter 7: Defining Models 75 In ma
Chapter 7: Defining Models 77 Here
Chapter 7: Defining Models 79 class
Chapter 7: Defining Models 81 Here
Chapter 7: Defining Models 83 } } )
Chapter 7: Defining Models 85 --- #
Chapter 7: Defining Models 87 } { }
Chapter 7: Defining Models 89 } } )
Chapter 7: Defining Models 91 Some
Chapter 7: Defining Models 93 Now w
Chapter 7: Defining Models 95 # ...
Chapter 7: Defining Models 97 Concl
Chapter 8: Working with Models 99 $
Chapter 8: Working with Models 101
Chapter 8: Working with Models 103
Chapter 8: Working with Models 105
Chapter 8: Working with Models 107
Chapter 8: Working with Models 109
Chapter 8: Working with Models 111
Chapter 8: Working with Models 113
Chapter 8: Working with Models 115
Chapter 8: Working with Models 117
Chapter 8: Working with Models 119
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 9: DQL (Doctrine Query Lang
Chapter 10: Component Overview 163
Chapter 10: Component Overview 165
Chapter 10: Component Overview 167
Chapter 10: Component Overview 169
Chapter 10: Component Overview 171
Chapter 10: Component Overview 173
Chapter 18: Hierarchical Data 277 N
Chapter 18: Hierarchical Data 279 i
Chapter 18: Hierarchical Data 281 E
Chapter 18: Hierarchical Data 283 F
Chapter 19: Data Fixtures 285 Chapt
Chapter 19: Data Fixtures 287 class
Chapter 19: Data Fixtures 289 Resou
Chapter 19: Data Fixtures 291 --- #
Chapter 20: Database Abstraction La
Chapter 20: Database Abstraction La
Chapter 20: Database Abstraction La
Chapter 20: Database Abstraction La
Chapter 20: Database Abstraction La
Chapter 20: Database Abstraction La
Chapter 21: Transactions 305 log 43
Chapter 21: Transactions 307 try {
Chapter 22: Event Listeners 309 Cha
Chapter 22: Event Listeners 311 pub
Chapter 22: Event Listeners 313 pos
Chapter 22: Event Listeners 315 } {
Chapter 22: Event Listeners 317 */
Chapter 22: Event Listeners 319 FRO
Chapter 22: Event Listeners 321 Ski
Chapter 23: Caching 323 $cacheDrive
Chapter 23: Caching 325 // bootstra
Chapter 24: Migrations 327 Chapter
Chapter 24: Migrations 329 array('t
Chapter 24: Migrations 331 { } // .
Chapter 24: Migrations 333 $options
Chapter 24: Migrations 335 Up/Down
Chapter 24: Migrations 337 Now you
Chapter 25: Extensions 339 $ cd /pa
Chapter 26: Utilities 341 $pager->g
Chapter 26: Utilities 343 Currently
Chapter 26: Utilities 345 Template
Chapter 26: Utilities 347 Doctrine_
Chapter 26: Utilities 349 // Possib
Chapter 26: Utilities 351 // Genera
Chapter 26: Utilities 353 You can r
Chapter 26: Utilities 355 The above
Chapter 27: Unit Testing 357 $ cd /
Chapter 27: Unit Testing 359 } // .
Chapter 27: Unit Testing 361 Common
Chapter 28: Improving Performance 3
Chapter 28: Improving Performance 3
Chapter 28: Improving Performance 3
Chapter 29: Technology 369 Doctrine
Chapter 29: Technology 371 • Neve
Chapter 30: Exceptions and Warnings
Chapter 31: Real World Examples 375
Chapter 31: Real World Examples 377
Chapter 31: Real World Examples 379
Chapter 32: Coding Standards 381 Ch
Chapter 32: Coding Standards 383 Fu
Chapter 32: Coding Standards 385 $s
Chapter 32: Coding Standards 387 Fu