Chapter 10: Component Overview 174 Listing 10-49 // test.php // ... $user->refresh(); Refreshing relationships The Doctrine_Record::refresh() method can also refresh the already loaded record relationships, but you need to specify them on the original query. First lets retrieve a User with its associated Groups: Listing 10-50 // test.php // ... $q = Doctrine_Query::create() ->from('User u') ->leftJoin('u.Groups') ->where('id = ?'); $user = $q->fetchOne(array(1)); Now lets retrieve a Group with its associated Users: Listing 10-51 // test.php // ... $q = Doctrine_Query::create() ->from('Group g') ->leftJoin('g.Users') ->where('id = ?'); $group = $q->fetchOne(array(1)); Now lets link the retrieved User and Group through a UserGroup instance: Listing 10-52 // test.php // ... $userGroup = new UserGroup(); $userGroup->user_id = $user->id; $userGroup->group_id = $group->id; $userGroup->save(); You can also link a User to a Group in a much simpler way, by simply adding the Group to the User. Doctrine will take care of creating the UserGroup instance for you automatically: Listing 10-53 // test.php // ... $user->Groups[] = $group; $user->save() Now if we call Doctrine_Record::refresh(true) it will refresh the record and its relationships loading the newly created reference we made above: Listing 10-54 // test.php ----------------- Brought to you by
Chapter 10: Component Overview 175 // ... $user->refresh(true); $group->refresh(true); You can also lazily refresh all defined relationships of a model using Doctrine_Record::refreshRelated(): // test.php Listing 10-55 // ... $user = Doctrine_Core::getTable('User')->findOneByName('jon'); $user->refreshRelated(); If you want to refresh an individual specified relationship just pass the name of a relationship to the refreshRelated() function and it will lazily load the relationship: // test.php Listing 10-56 // ... $user->refreshRelated('Phonenumber'); Deleting Records Deleting records in Doctrine is handled by Doctrine_Record::delete(), Doctrine_Collection::delete() and Doctrine_Connection::delete() methods. // test.php Listing 10-57 // ... $userTable = Doctrine_Core::getTable("User"); $user = $userTable->find(2); // deletes user and all related composite objects if($user !== false) { $user->delete(); } If you have a Doctrine_Collection of User records you can call delete() and it will loop over all records calling Doctrine_Record::delete() for you. // test.php Listing 10-58 // ... $users = $userTable->findAll(); Now you can delete all users and their related composite objects by calling Doctrine_Collection::delete(). It will loop over all Users in the collection calling delete one each one: // test.php Listing 10-59 // ... $users->delete(); ----------------- 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 13: Data Validation 225 //
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