Chapter 10: Component Overview 176 Using Expression Values There might be situations where you need to use SQL expressions as values of columns. This can be achieved by using Doctrine_Expression which converts portable DQL expressions to your native SQL expressions. Lets say we have a class called event with columns timepoint(datetime) and name(string). Saving the record with the current timestamp can be achieved as follows: Listing 10-60 // test.php // ... $user = new User(); $user->username = 'jwage'; $user->updated_at = new Doctrine_Expression('NOW()'); $user->save(); The above code would issue the following SQL query: Listing 10-61 INSERT INTO user (username, updated_at_) VALUES ('jwage', NOW()) When you use Doctrine_Expression with your objects in order to get the updated value you will have to manually call refresh() to get the updated value from the database. Listing 10-62 // test.php // ... $user->refresh(); Getting Record State Every Doctrine_Record has a state. First of all records can be transient or persistent. Every record that is retrieved from database is persistent and every newly created record is considered transient. If a Doctrine_Record is retrieved from database but the only loaded property is its primary key, then this record has a state called proxy. Every transient and persistent Doctrine_Record is either clean or dirty. Doctrine_Record is clean when none of its properties are changed and dirty when at least one of its properties has changed. A record can also have a state called locked. In order to avoid infinite recursion in some rare circular reference cases Doctrine uses this state internally to indicate that a record is currently under a manipulation operation. Below is a table containing all the different states a record can be in with a short description of it: Name Doctrine_Record::STATE_PROXY Description Record is in proxy state meaning its persistent but not all of its properties are loaded from the database. Doctrine_Record::STATE_TCLEAN Record is transient clean, meaning its transient and none of its properties are changed. Doctrine_Record::STATE_TDIRTY Record is transient dirty, meaning its transient and some of its properties are changed. Doctrine_Record::STATE_DIRTY Record is dirty, meaning its persistent and some of its properties are changed. ----------------- Brought to you by
Chapter 10: Component Overview 177 Doctrine_Record::STATE_CLEAN Doctrine_Record::STATE_LOCKED Record is locked. Record is clean, meaning its persistent and none of its properties are changed. You can easily get the state of a record by using the Doctrine_Record::state() method: // test.php Listing 10-63 // ... $user = new User(); if ($user->state() == Doctrine_Record::STATE_TDIRTY) { echo 'Record is transient dirty'; } The above object is TDIRTY because it has some default values specified in the schema. If we use an object that has no default values and instantiate a new instance it will return TCLEAN. // test.php Listing 10-64 // ... $account = new Account(); if ($account->state() == Doctrine_Record::STATE_TCLEAN) { echo 'Record is transient clean'; } Getting Object Copy Sometimes you may want to get a copy of your object (a new object with all properties copied). Doctrine provides a simple method for this: Doctrine_Record::copy(). // test.php Listing 10-65 // ... $copy = $user->copy(); Notice that copying the record with copy() returns a new record (state TDIRTY) with the values of the old record, and it copies the relations of that record. If you do not want to copy the relations too, you need to use copy(false). Get a copy of user without the relations // test.php Listing 10-66 // ... $copy = $user->copy(false); Using the PHP clone functionality simply uses this copy() functionality internally: // test.php Listing 10-67 // ... $copy = clone $user; ----------------- 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 14: Data Hydrators 227 Chap
Chapter 14: Data Hydrators 229 echo
Chapter 15: Inheritance 231 Chapter
Chapter 15: Inheritance 233 columns
Chapter 15: Inheritance 235 name: s
Chapter 15: Inheritance 237 // test
Chapter 16: Behaviors 239 Chapter 1
Chapter 16: Behaviors 241 Here is t
Chapter 16: Behaviors 243 } $this->
Chapter 16: Behaviors 245 The imple
Chapter 16: Behaviors 247 } } ); )
Chapter 16: Behaviors 249 $blogPost
Chapter 16: Behaviors 251 Look how
Chapter 16: Behaviors 253 length 25
Chapter 16: Behaviors 255 $ php tes
Chapter 16: Behaviors 257 Searchabl
Chapter 16: Behaviors 259 // test.p
Chapter 16: Behaviors 261 } } elsei
Chapter 16: Behaviors 263 ) [name]
Chapter 16: Behaviors 265 } public
Chapter 17: Searching 267 Chapter 1
Chapter 17: Searching 269 body LONG
Chapter 17: Searching 271 • Strip
Chapter 17: Searching 273 $ php tes
Chapter 17: Searching 275 // test.p
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