Doctrine Manual Konsta Vesterinen    Rev. 4167   April 2, 2008
Doctrine Manual2
Contents1 Getting started                                                                                                 ...
Contents                                                                                                               Doc...
Doctrine Manual                                                                                                           ...
Contents                                                                                                      Doctrine Man...
Doctrine Manual                                                                                                           ...
Contents                                                                                                                  ...
Doctrine Manual                                                                                                           ...
Contents                                                                                                                  ...
Doctrine Manual                                                                                                           ...
Contents                                                                                                                 D...
Chapter 1Getting started1.1     RequirementsDoctrine requires PHP >= 5.2. it doesn’t require any external libraries. For d...
Chapter 1. Getting started                                                  Doctrine Manualsimplescript for managing a ”Us...
Doctrine Manual                                                     1.3. Starting new project1.2.4     PEARYou can install...
Chapter 1. Getting started                                                               Doctrine Manual           $this -...
Doctrine Manual                                          1.4. Working with existing databases1.4      Working with existin...
Chapter 1. Getting started                                                           Doctrine Manual         $this - > set...
Doctrine Manual                                                               1.5. Creating tables}This is where you can p...
Chapter 1. Getting started                                                      Doctrine Manual// in order to export we ne...
Doctrine Manual                                                               1.6. Generating models$manager - > setAttrib...
Chapter 1. Getting started                                                     Doctrine Manualpath is then used in theDoct...
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Manual doctrine jog
Upcoming SlideShare
Loading in...5
×

Manual doctrine jog

3,219

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,219
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Manual doctrine jog"

  1. 1. Doctrine Manual Konsta Vesterinen Rev. 4167 April 2, 2008
  2. 2. Doctrine Manual2
  3. 3. Contents1 Getting started 13 1.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Sandbox Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.2 SVN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.3 SVN externals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.2.4 PEAR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.2.5 Zip-package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.3 Starting new project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4 Working with existing databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4.2 Making the first import . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.5 Creating tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.5.2 Getting export queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.5.3 Export options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.6 Generating models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.7 Auto loading models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.7.1 Conservative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.7.2 Aggressive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.8 Command line interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.9 My first project tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.9.2 Download . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.9.3 Package Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.9.4 Running the CLI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.9.5 Defining Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.9.6 Test Data Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.9.7 Building Everything . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.9.8 Running Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.9.9 User CRUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Connection management 31 2.1 DSN, the Data Source Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.1.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.2 Opening a new connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.3 Lazy-connecting to database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.4 Managing connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.5 Connection-component binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
  4. 4. Contents Doctrine Manual3 Basic schema mapping 37 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.2 Table and class naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.3 Table options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.4 Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.4.1 Column naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.4.2 Column aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.4.3 Default values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4.4 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.4.4.2 Type modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.4.4.3 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.4.4.4 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.4.4.5 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.4.4.6 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.4.4.7 Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.4.4.8 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.4.4.9 Blob . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.4.4.10 Clob . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.4.4.11 Timestamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.4.4.12 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.4.4.13 Date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.4.4.14 Enum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.4.4.15 Gzip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.4.4.16 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.5 Constraints and validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.5.2 Notnull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.5.3 Unique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.5.4 Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3.6 Record identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.6.2 Natural . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.6.3 Autoincremented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.6.4 Composite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.6.5 Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.7 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.7.2 Adding indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.7.3 Index options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.7.4 Special indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664 Relations 67 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.2 Foreign key associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 4.2.1 One-To-One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 4.2.2 One-to-Many, Many-to-One . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4.2.3 Tree structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.3 Join table associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.3.1 Many-to-Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.3.2 Self-referencing (Nest relations) . . . . . . . . . . . . . . . . . . . . . . . . 75 4.3.2.1 Non-equal nest relations . . . . . . . . . . . . . . . . . . . . . . . 75 4.3.2.2 Equal nest relations . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
  5. 5. Doctrine Manual Contents 4.4.1 Simple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.4.2 Class table inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 4.4.3 Concrete inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4.4.4 Column aggregation inheritance . . . . . . . . . . . . . . . . . . . . . . . 82 4.5 Foreign key constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 4.5.2 Integrity actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855 Schema Files 89 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.2 Short Hand Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.3 Expanded Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.4 Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.4.1 Detect Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.4.2 Customizing Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 5.4.3 One to One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 5.4.4 One to Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 5.4.5 Many to Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.5 Features & Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.5.1 Connection Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.5.2 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5.5.3 Enums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5.5.4 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.5.5 ActAs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.5.6 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 5.5.7 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 5.5.8 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.5.9 Generate Accessors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 5.5.10 Column Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.5.11 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.5.12 Global Schema Information . . . . . . . . . . . . . . . . . . . . . . . . . . 99 5.6 Using Schema Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006 Utilities 103 6.1 Pagination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6.1.2 Working with pager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 6.1.3 Controlling range styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 6.1.3.1 Sliding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 6.1.3.2 Jumping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 6.1.4 Advanced layouts with pager . . . . . . . . . . . . . . . . . . . . . . . . . 108 6.1.4.1 Mask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 6.1.4.2 Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 6.1.5 Customizing pager layout . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 6.2 Facade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6.2.1 Creating & Dropping Databases . . . . . . . . . . . . . . . . . . . . . . . 114 6.2.2 Convenience Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6.2.3 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 6.3 Command Line Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6.3.2 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 6.3.3 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 6.4 Sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.4.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5
  6. 6. Contents Doctrine Manual7 Working with objects 121 7.1 Dealing with relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 7.1.1 Creating related records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 7.1.2 Retrieving related records . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 7.1.3 Updating related records . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.1.4 Deleting related records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.1.5 Working with related records . . . . . . . . . . . . . . . . . . . . . . . . . 125 7.1.5.1 Testing the existance of a relation . . . . . . . . . . . . . . . . . 125 7.2 Many-to-Many relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 7.2.1 Creating a new link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 7.2.2 Deleting a link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 7.3 Fetching objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 7.3.1 Field lazy-loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 7.4 Arrays and objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 7.4.1 toArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 7.4.2 fromArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 7.4.3 synchronizeWithArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7.5 Overriding the constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318 Component overview 133 8.1 Record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 8.1.1 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 8.1.2 Retrieving existing records . . . . . . . . . . . . . . . . . . . . . . . . . . 136 8.1.3 Updating records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 8.1.4 Refreshing records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 8.1.4.1 Refreshing relationships . . . . . . . . . . . . . . . . . . . . . . . 137 8.1.5 Deleting records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.1.6 Using expression values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.1.7 Getting record state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 8.1.8 Getting object copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 8.1.9 Saving a blank record . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 8.1.10 Mapping custom values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 8.1.11 Serializing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 8.1.12 Checking existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 8.1.13 Function callbacks for columns . . . . . . . . . . . . . . . . . . . . . . . . 141 8.2 Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 8.2.1 Accessing elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 8.2.2 Adding new elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 8.2.3 Getting collection count . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 8.2.4 Saving the collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 8.2.5 Deleting collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 8.2.6 Key mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 8.2.7 Loading related records . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 8.3 Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 8.3.1 Available drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 8.3.2 Getting a table object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 8.3.3 Flushing the connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 8.3.4 Querying the database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 8.4 Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 8.4.1 Getting column information . . . . . . . . . . . . . . . . . . . . . . . . . . 148 8.4.2 Getting relation information . . . . . . . . . . . . . . . . . . . . . . . . . . 148 8.4.3 Finder methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 8.4.3.1 Custom table classes . . . . . . . . . . . . . . . . . . . . . . . . . 149 8.4.4 Custom finders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1496
  7. 7. Doctrine Manual Contents 8.5 Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 8.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 8.5.2 More Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 8.5.3 Valid or Not Valid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 8.5.3.1 Implicit validation . . . . . . . . . . . . . . . . . . . . . . . . . . 152 8.5.3.2 Explicit validation . . . . . . . . . . . . . . . . . . . . . . . . . . 153 8.6 Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 8.6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 8.6.2 Basic usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8.7 Locking manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8.7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8.7.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 8.7.3 Technical Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8.8 View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8.8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8.8.2 Managing views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 8.8.3 Using views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1579 Hierarchical data 159 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 9.2 Nested set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 9.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 9.2.2 Setting up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 9.2.3 More than 1 tree in a single table . . . . . . . . . . . . . . . . . . . . . . . 161 9.2.4 Working with the tree(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 9.2.4.1 Creating a root node . . . . . . . . . . . . . . . . . . . . . . . . 162 9.2.4.2 Inserting a node . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 9.2.4.3 Deleting a node . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 9.2.4.4 Moving a node . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 9.2.4.5 Examining a node . . . . . . . . . . . . . . . . . . . . . . . . . . 163 9.2.4.6 Examining and retrieving siblings . . . . . . . . . . . . . . . . . 163 9.2.4.7 Examining and retrieving children / parents / descendants / an- cestors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 9.2.4.8 Simple Example: Displaying a tree . . . . . . . . . . . . . . . . . 164 9.2.5 Advanced usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 9.2.5.1 Fetching a tree with relations . . . . . . . . . . . . . . . . . . . . 165 9.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16610 Configuration 167 10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 10.2 Levels of configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 10.3 General attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 10.3.1 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 10.3.1.1 Portability Mode Constants . . . . . . . . . . . . . . . . . . . . . 168 10.3.1.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 10.3.2 Identifier quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 10.3.3 Exporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 10.3.4 Event listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.4 Naming convention attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.4.1 Index name format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.4.2 Sequence name format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 10.4.3 Table name format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 10.4.4 Database name format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 10.5 Validation attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7
  8. 8. Contents Doctrine Manual 10.5.1 Validation mode constants . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 10.5.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17411 Data fixtures 175 11.1 Exporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 11.2 Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 11.3 Dummy Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 11.4 Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 11.5 Fixtures For Nested Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 11.6 Fixtures For I18n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17812 DQL (Doctrine Query Language) 179 12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 12.2 SELECT queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 12.2.1 DISTINCT keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 12.2.2 Aggregate values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 12.3 UPDATE queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 12.4 DELETE queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 12.5 FROM clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 12.6 JOIN syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 12.6.1 ON keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 12.6.2 WITH keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 12.7 INDEXBY keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 12.8 WHERE clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 12.9 Conditional expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 12.9.1 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 12.9.2 Input parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 12.9.3 Operators and operator precedence . . . . . . . . . . . . . . . . . . . . . . 188 12.9.4 Between expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.9.5 In expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.9.6 Like Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.9.7 Null Comparison Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 190 12.9.8 Empty Collection Comparison Expressions . . . . . . . . . . . . . . . . . 190 12.9.9 Collection Member Expressions . . . . . . . . . . . . . . . . . . . . . . . . 190 12.9.10 Exists Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 12.9.11 All and Any Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 12.9.12 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 12.10Functional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 12.10.1 String functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 12.10.2 Arithmetic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 12.10.3 Datetime functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.2 Comparisons using subqueries . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.3 Conditional expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.3.1 ANY, IN and SOME . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.3.2 ALL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.3.3 EXISTS and NOT EXISTS . . . . . . . . . . . . . . . . . . . . . 195 12.11.4 Correlated subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.11.5 Subqueries in FROM clause . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.12GROUP BY, HAVING clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 12.13ORDER BY clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 12.13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 12.13.2 Sorting by an aggregate value . . . . . . . . . . . . . . . . . . . . . . . . . 1978
  9. 9. Doctrine Manual Contents 12.13.3 Using random order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 12.14LIMIT and OFFSET clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 12.14.1 Driver portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 12.14.2 The limit-subquery-algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 198 12.15Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 12.16The Query Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 12.16.1 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 12.17BNF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 12.18Magic Finders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20413 Native SQL 205 13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 13.2 Component queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 13.3 Fetching from multiple components . . . . . . . . . . . . . . . . . . . . . . . . . . 20614 Transactions 209 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 14.2 Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 14.3 Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 14.4 Locking strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 14.4.1 Pessimistic locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 14.4.2 Optimistic locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 14.5 Lock modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 14.6 Isolation levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 14.7 Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21315 Caching 215 15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 15.2 Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 15.2.1 Memcache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 15.2.2 APC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.2.3 Db . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.3 Query Cache & Result Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.3.2 Query Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.2.1 Using the query cache . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.2.2 Fine-tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.3 Result Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.3.1 Using the result cache . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.3.2 Fine-tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22016 Event listeners 221 16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 16.2 Connection listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 16.2.1 Creating a new listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 16.2.2 Attaching listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 16.2.3 preConnect, postConnect . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 16.2.4 Transaction listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 16.2.5 Query execution listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 16.3 Query listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 16.4 Record listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 16.5 Record hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 16.6 Chaining listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 16.7 The Event object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 9
  10. 10. Contents Doctrine Manual 16.7.1 Getting the invoker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 16.7.2 Event codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 16.7.3 getInvoker() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 16.7.4 skipOperation() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 16.7.5 skipNextListener() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23117 Class templates 233 17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 17.2 Simple templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 17.3 Templates with relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 17.4 Delegate methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 17.5 Working with multiple templates . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 17.6 Core Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 17.6.1 Versionable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 17.6.2 Timestampable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 17.6.3 Sluggable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 17.6.4 I18n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 17.6.5 NestedSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 17.6.6 Searchable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 17.6.7 Geographical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24218 Plugins 245 18.1 Internationalization with I18n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 18.1.1 Creating the I18n table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 18.1.2 Using I18n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 18.2 AuditLog and versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 18.2.1 Creating the version table . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 18.2.2 Using versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 18.2.3 Reverting changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 18.2.4 Advanced usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 18.3 Soft-delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 18.4 Creating plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 18.5 Nesting plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25119 File parser 253 19.1 Dumping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 19.2 Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25420 Migration 255 20.1 Writing Migration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 20.1.1 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 20.1.2 Altering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 20.2 Performing Migrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25721 Searching 259 21.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 21.2 Index structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 21.3 Index building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 21.4 Text analyzers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 21.5 Query language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 21.6 File searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26210
  11. 11. Doctrine Manual Contents22 Database abstraction 265 22.1 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2 Export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2.2 Creating a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2.3 Creating tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 22.2.4 Creating foreign keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 22.2.5 Altering table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 22.2.6 Creating indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 22.2.7 Deleting database elements . . . . . . . . . . . . . . . . . . . . . . . . . . 270 22.3 Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 22.3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 22.3.2 Listing databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 22.3.3 Listing sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 22.3.4 Listing constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 22.3.5 Listing table fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 22.3.6 Listing table indices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 22.3.7 Listing tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 22.3.8 Listing views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 22.4 DataDict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 22.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 22.4.2 Getting portable declaration . . . . . . . . . . . . . . . . . . . . . . . . . 274 22.4.3 Getting native declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 22.5 Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 22.5.1 Mysql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 22.5.1.1 Setting table type . . . . . . . . . . . . . . . . . . . . . . . . . . 27423 Improving Performance 277 23.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 23.2 Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 23.3 Fetch only what you need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 23.4 Bundle your class files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 23.5 Use a bytecode cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 23.6 Other tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28124 Technology 283 24.1 Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 24.2 Design patterns used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 24.3 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 24.4 Internal optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28625 Exceptions and warnings 287 25.1 Manager exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 25.2 Relation exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 25.3 Connection exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 25.4 Query exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28826 Real world examples 289 26.1 User management system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 26.2 Forum application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 11
  12. 12. Contents Doctrine Manual27 Coding standards 295 27.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.1.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.1.2 Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.2 PHP File Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.2.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.2.2 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.2.3 Maximum line length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.2.4 Line termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 27.3 Naming Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 27.3.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 27.3.2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 27.3.3 Filenames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 27.3.4 Functions and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 27.3.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 27.3.6 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 27.3.7 Record columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 27.4 Coding Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 27.4.1 PHP code demarcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 27.4.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 27.4.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 27.4.4 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 27.4.5 Functions and methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 27.4.6 Control statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 27.4.7 Inline documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 27.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 27.5.1 Writing tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 27.5.1.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 27.5.1.2 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 27.5.1.3 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712
  13. 13. Chapter 1Getting started1.1 RequirementsDoctrine requires PHP >= 5.2. it doesn’t require any external libraries. For database functioncall abstractionDoctrine uses PDO which is bundled with php by default.1.2 InstallationThere are currently four different methods to install Doctrine. • SVN (subversion) • SVN externals • Pear • Zip-packageIt is recommended to download Doctrine via SVN (subversion), because in this case updatingis easy.If your project is already under version control with SVN, you should choose SVN externals.If you wish to just try out Doctrine in under 5 minutes, the sandbox package is recommended.1.2.1 Sandbox PackageDoctrine also provides a special package which is a zero configuration Doctrine implementation.Itincludes a fully featured command line interface for managing your schema files, migrations,database connections, data fixtures, and many other features. You can read about the sandboxpackageand how to use it in the 6 chapter under the Sandbox section.Below you will find the url to a tutorial on how to how to get started usingDoctrine with the sandbox package. With the sandbox and this tutorial you can get Doctrineupand running in under 5 minutes. The tutorial offers example schema files, data fixtures, and a
  14. 14. Chapter 1. Getting started Doctrine Manualsimplescript for managing a ”User” model with Doctrine. Simple create, update, delete functionality.The tutorial can be found here http://trac.phpdoctrine.org/wiki/MyFirstProject and thesandbox packagecan be downloaded from here http://www.phpdoctrine.org/download1.2.2 SVNThe installation of doctrine via SVN is very easy. Just get the latest revision of Doctrine fromhttp://svn.phpdoctrine.org/branches/0.10.In order to check out Doctrine in the current directory using the svn command line tool use thefollowing code: Listing 1.1:svn co http :// svn . phpdoctrine . org / branches /0.10 .If you do not have a SVN client, chose one from the list below. Find the Checkout option andentersvn.phpdoctrine.org/branches/0.10 in the path or repository url parameter. There is no needfor a usernameor password to check out Doctrine. • TortoiseSVN1 a Windows application that integrates into Windows Explorer • svnx2 a Mac OS X GUI svn application • Eclipse has SVN integration through the subeclipse3 pluginYou can update to the latest version with Listing 1.2:svn updatein your doctrine directory.1.2.3 SVN externalsIf your project is under version control with SVN, you should set up doctrine via svn externals.You can do this withthe svn command line tool: Listing 1.3:svn pe svn : externals / path / to / projectYou have to put the following line in the editor and save the changes. Listing 1.4:doctrine http :// svn . phpdoctrine . org / branches /0.10Afterwards you can download doctrine with Listing 1.5:svn update 1 http://tortoisesvn.tigris.org/ 2 http://www.apple.com/downloads/macosx/development tools/svnx.html 3 http://subclipse.tigris.org/14
  15. 15. Doctrine Manual 1.3. Starting new project1.2.4 PEARYou can install Doctrine via PEAR with the following command: Listing 1.6:pear install http :// pear . phpdoctrine . org / Doctrine -0.10.21.2.5 Zip-packageYou can download Doctrine as a .zip or .tgz (for Linux) package from http://www.phpdoctrine.org/download.Simply unzip it to your project directory with your favorite zip tool.Under Linux you can extract the .tgz package with the following command line instruction: Listing 1.7:tar xzf Doctrine -0.10.1. tgz1.3 Starting new projectDoctrine Record is the basic component of every doctrine-based project. There should be atleastone Doctrine Record foreach of your database tables. Doctrine Record follows the [http://www.martinfowler.com/eaaCatalog/activeRecord.htmlActive Record pattern]Doctrine always adds a primary key column named ’id’ to tables that doesn’t have any primarykeys specified. Only thingyou need to for creating database tables is defining a class which extends Doctrine Record andsetting a setTableDefinitionmethod with hasColumn() method calls and by exporting those classes.Lets say we want to create a database table called ’user’ with columns id(primary key), name,username, password andcreated. Provided that you have already installed Doctrine these few lines of code are all youneed:User.php : Listing 1.8:<? phpclass User extends Doctrine_Record{ public function setTableDefinition () { // set ’ user ’ table columns , note that // id column is auto - created as no primary key is specified $this - > hasColumn ( ’ name ’ , ’ string ’ ,30) ; $this - > hasColumn ( ’ username ’ , ’ string ’ ,20) ; $this - > hasColumn ( ’ password ’ , ’ string ’ ,16) ; } public function setUp () { 15
  16. 16. Chapter 1. Getting started Doctrine Manual $this - > actAs ( ’ Timestampable ’) ; }}?>You can alternatively specify your Doctrine schema information as a YAML schema file. Belowis an example user.yml filewhich you can generate your Doctrine Record from. Listing 1.9:---User : actAs : [ Timestampable ] columns : name : string (30) username : string (20) password : string (16)You can generate the php code from the yaml with the following code. Listing 1.10:<? phpDoctrine : : g e ne ra teM od el sFr om Ya ml ( ’/ path / to / user . yml ’ , ’/ path / to / generate / models ) ;?>Have a look in /path/to/generate/models/ and /path/to/generate/models/generated. You willsee User.php and BaseUser.class.php.User.php is for you to add your own custom functionality, and BaseUser.php is the code whichis automatically regeneratedfrom the YAML schema file above each time.Now that we have a Doctrine Record class, we can export it to the database and create thetables. For exporting the userclass into database we need a simple build script: Listing 1.11:<? php// require the base Doctrine classrequire_once ( ’ path - to - doctrine / lib / Doctrine . php ’) ;// register the autoloadersp l _ a u t o l o a d _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ;require_once ( ’ User . php ’) ;// set up a connectionDoctrine_Manager : : connection ( ’ mysql : // user : pass @ localhost / test ’) ;// export the classesDoctrine : : c r e ateT ables From Array ( array ( ’ User ’) ) ;?>We now have a user model that supports basic CRUD opperations!16
  17. 17. Doctrine Manual 1.4. Working with existing databases1.4 Working with existing databases1.4.1 IntroductionA common case when looking for ORM tools like Doctrine is that the database and the codethat access it is growinglarge/complex. A more substantial tool is needed then manual SQL code.Doctrine has support for generating Doctrine Record classes from your existing database. Thereis no need for you tomanually write all the Doctrine Record classes for your domain model.1.4.2 Making the first importLet’s consider we have a mysql database called test with a single table called ’file’.The file table has been created with the following sql statement: Listing 1.12:CREATE TABLE file ( id INT UNSIGNED AUTO_INCREMENT NOT NULL , name VARCHAR (150) , size BIGINT , modified BIGINT , type VARCHAR (10) , content TEXT , path TEXT , PRIMARY KEY ( id ) )Now we would like to convert it into Doctrine record class. It can be achieved easily with thefollowing code snippet: Listing 1.13:<? phprequire_once ( ’ path - to - doctrine / lib / Doctrine . php ’) ;s p l _ a u t oload _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ;Doctrine_Manager : : connection ( ’ mysql : // root : dc34 @ localhost / test ’) ;// import method takes one parameter : the import directory ( the directory where// the generated record files will be put inDoctrine : : generateModelsFromDb ( ’ myrecords ’) ;?>That’s it! Now there should be a file called BaseFile.php in your myrecords/generated directory.The file should look like: Listing 1.14:<? php/* * * This class has been auto - generated by the Doctrine ORM Framework */abstract class BaseFile extends Doctrine_Record{ public function setTableDefinition () { 17
  18. 18. Chapter 1. Getting started Doctrine Manual $this - > setTableName ( ’ file ’) ; $this - > hasColumn ( ’ id ’ , ’ integer ’ , 4 , array ( ’ alltypes ’ = > array ( 0 = > ’ integer ’ , ) , ’ ntype ’ = > ’ int (10) unsigned ’ , ’ unsigned ’ = > 1 , ’ values ’ = > array () , ’ primary ’ = > true , ’ notnull ’ = > true , ’ autoincrement ’ = > true )); $this - > hasColumn ( ’ name ’ , ’ string ’ , 150 , array ( ’ alltypes ’ = > array ( 0 = > ’ string ’ , ) , ’ ntype ’ = > ’ varchar (150) ’ , ’ fixed ’ = > false , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; $this - > hasColumn ( ’ size ’ , ’ integer ’ , 8 , array ( ’ alltypes ’ = > array ( 0 = > ’ integer ’ , ) , ’ ntype ’ = > ’ bigint (20) ’ , ’ unsigned ’ = > 0 , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; $this - > hasColumn ( ’ modified ’ , ’ integer ’ , 8 , array ( ’ alltypes ’ = > array ( 0 = > ’ integer ’ , ) , ’ ntype ’ = > ’ bigint (20) ’ , ’ unsigned ’ = > 0 , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; $this - > hasColumn ( ’ type ’ , ’ string ’ , 10 , array ( ’ alltypes ’ = > array ( 0 = > ’ string ’ , ) , ’ ntype ’ = > ’ varchar (10) ’ , ’ fixed ’ = > false , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; $this - > hasColumn ( ’ content ’ , ’ string ’ , null , array ( ’ alltypes ’ = > array ( 0 = > ’ string ’ , 1 = > ’ clob ’ , ) , ’ ntype ’ = > ’ text ’ , ’ fixed ’ = > false , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; $this - > hasColumn ( ’ path ’ , ’ string ’ , null , array ( ’ alltypes ’ = > array ( 0 = > ’ string ’ , 1 = > ’ clob ’ , ) , ’ ntype ’ = > ’ text ’ , ’ fixed ’ = > false , ’ values ’ = > array () , ’ primary ’ = > false , ’ notnull ’ = > false , ’ autoincrement ’ = > false ) ) ; } public function setUp () { parent : : setUp () ; }}You should also have a file called File.php in your myrecords directory. The file should look like: Listing 1.15:<? php/* * * This class has been auto - generated by the Doctrine ORM Framework */class File extends BaseFile{}?>Doctrine will automatically generate a skeleton Doctrine Table class for the model at myrecord-s/UserTable.php. The fileshould look like: Listing 1.16:<? php/* * * This class has been auto - generated by the Doctrine ORM Framework */class FileTable extends Doctrine_Table{18
  19. 19. Doctrine Manual 1.5. Creating tables}This is where you can put your custom finder methods which can be used by calling Doc-trine::getTable(’User’).1.5 Creating tables1.5.1 IntroductionDoctrine supports exporting record classes into database. This means that based on the defini-tions given in your recordclasses Doctrine will create the tables in your database.Lets say we have a classes called User and Phonenumber with the following definitions: Listing 1.17:<? php// file User . phpclass User extends Doctrine_Record{ public function setTableDefinition () { $this - > hasColumn ( ’ name ’ , ’ string ’ , 20) ; } public function setUp () { $this - > hasMany ( ’ Phonenumber ’ , array ( ’ local ’ = > ’ id ’ , ’ foreign ’ = > ’ user_id ’) ) ; }}// file Phonenumber . phpclass Phonenumber extends Doctrine_Record{ public function setTableDefinition () { $this - > hasColumn ( ’ phonenumber ’ , ’ string ’ , 20) ; $this - > hasColumn ( ’ user_id ’ , ’ integer ’) ; } public function setUp () { $this - > hasOne ( ’ User ’ , array ( ’ local ’ = > ’ user_id ’ , ’ foreign ’ = > ’ id ’ , ’ onDelete ’ = > ’ CASCADE ’) ) ; }}?>Now lets say these classes are in directory ’models/’. We can make Doctrine to iterate throughthis directory andattach these classes into your database structure with the following script: Listing 1.18:<? phprequire_once ( ’ path - to - doctrine / lib / Doctrine . php ’) ;s p l _ a u t oload _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ; 19
  20. 20. Chapter 1. Getting started Doctrine Manual// in order to export we need a database connectionDoctrine_Manager : : connection ( ’ mysql : // user : pass @ localhost / test ’) ;Doctrine : : c r ea te Tab le sF rom Mo de ls ( ’ models ’) ;?>This would execute the following queries on mysql. Listing 1.19:CREATE TABLE user ( id BIGINT AUTO_INCREMENT , name VARCHAR (20) , PRIMARY KEY ( id ) , INDEX ( id ) ) ;CREATE TABLE phonenumber ( id INT AUTO_INCREMENT , phonenumber VARCHAR (20) , user_id BIGINT , PRIMARY KEY ( id ) , INDEX ( user_id ) ) ;ALTER TABLE phonenumber ADD CONSTRAINT FOREIGN KEY ( user_id ) REFERENCES user ( id ) ON DELETE CASCADE ;Pay attention to the following things: 1. The autoincrement primary key columns are auto-added since we didn’t specify any pri- mary key columns 2. Doctrine auto-adds indexes to the referenced relation columns (this is needed in mysql)1.5.2 Getting export queriesThere might be situations where you don’t want to execute the export queries immediatelyrather you want to get thequery strings and maybe attach them to a build.sql file. This can be easily achieved as follows: Listing 1.20:<? phprequire_once ( ’ path - to - doctrine / lib / Doctrine . php ’) ;sp l _ a u t o l o a d _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ;Doctrine_Manager : : connection ( ’ mgsql : // user : pass @ localhost / test ’) ;$queries = Doctrine : : gen erat eSqlF romM odels ( ’ models ’) ;echo $queries ;?>Consider the same situation and you want to get the string of sql queries needed to perform theexporting. It can beachieved with Doctrine::generateSqlFromModels().1.5.3 Export options Listing 1.21:<? php// export everything , table definitions and constraints$manager = Doctrine_Manager : : getInstance () ;20
  21. 21. Doctrine Manual 1.6. Generating models$manager - > setAttribute ( Doctrine : : ATTR_EXPORT , Doctrine : : EXPORT_ALL ) ;// export classes without constraints$manager - > setAttribute ( Doctrine : : ATTR_EXPORT , Doctrine : : EXPORT_TABLES ^ Doctrine : : EXPORT_CONSTRAINTS ) ;// turn off exporting$manager - > setAttribute ( Doctrine : : ATTR_EXPORT , Doctrine : : EXPORT_NONE ) ;$sql = Doctrine : : gen erat eSqlF romM odels () ;?>1.6 Generating modelsDoctrine offers the ability to generate models from existing databases, or from YAML schemafiles. You already read aboutgenerating models from an existing database in the 1.4section.Here is a simple example of how to generate your models from yaml schema files.Create a schema files/user.yml and place the following yaml in the file Listing 1.22:---User : columns : username : string (255) password : string (255)Now we need can use a little script to generate the Doctrine Record definition. Listing 1.23:<? phprequire_once ( ’/ path / to / Doctrine . php ’) ;s p l _ a u t oload _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ;Doctrine : : ge ne ra teM od el sFr om Ya ml ( ’/ path / to / schema_files / ’ , ’/ path / to / generate / models ’) ;?>Now you will have models/User.php and models/generated/BaseUser.php. User.php is for youto add custom code to, it isonly generated once, and BaseUser.php is regenerated each time you call generateModels-FromYaml()1.7 Auto loading modelsDoctrine offers two ways of loading models. We have conservative(lazy) loading, and aggressiveloading. Conservative loadingwill not require the PHP file initially, instead it will cache the path to the class name and this 21
  22. 22. Chapter 1. Getting started Doctrine Manualpath is then used in theDoctrine::autoload() we registered earlier with spl autoload register(array(’Doctrine’, ’autoload’)).Below are some examplesusing the both types of model loading.1.7.1 ConservativeConservative model loading is going to be the ideal model loading method for a productionenvironment. This method will lazyload all of the models instead of loading them all when model loading is executed.Conservative model loading requires that each file contain only one class, and the file must benamed after the class. Example,if you have a class named User, it must be contained in a file named User.phpHere is an example of a basic Doctrine implementation using conservative model loading. Listing 1.24:<? php// require the base Doctrine classrequire_once ( ’ path - to - doctrine / lib / Doctrine . php ’) ;// register the autoloadersp l _ a u t o l o a d _reg ister ( array ( ’ Doctrine ’ , ’ autoload ’) ) ;Doctrine_Manager : : getInstance () - > setAttribute ( ’ model_loading ’ , ’ conservative ’) ;Doctrine : : loadModels ( ’/ path / to / models ’) ; // This call will not require the found . php files$user = new User () ; // This will invoke Doctrine : : autoload () to include the file so the User class is present .?>1.7.2 AggressiveAggressive model loading is the default model loading method and is very simple, it will lookfor all files with a .phpextension and will include it. Doctrine can not satisfy any inheritance and if your models extendanother model, it cannotinclude them in the correct order so it is up to you to make sure all dependencies are satisfiedin each class.With aggressive model loading you can have multiple classes per file, one class per file, and thefile name is notrequired to be related to the name of the class inside of the file.The downside of aggressive model loading is that every php file is included in every request, soif you have lots of modelsit is recommended you use conservative model loading.Here is an example of a basic Doctrine implementation using aggressive model loading. Listing 1.25:<? php// require the base Doctrine class22

×