Chapter 20: Database Abstraction Layer 292 Chapter 20 Database Abstraction Layer The Doctrine Database Abstraction Layer is the underlying framework that the ORM uses to communicate with the database and send the appropriate SQL depending on which database type you are using. It also has the ability to query the database for information like what table a database has or what fields a table has. This is how Doctrine is able to generate your models from existing databases so easily. This layer can be used independently of the ORM. This might be of use for example if you have an existing application that uses PDO directly and you want to port it to use the Doctrine Connections and DBAL. At a later phase you could begin to use the ORM for new things and rewrite old pieces to use the ORM. The DBAL is composed of a few different modules. In this chapter we will discuss the different modules and what their jobs are. Export The Export module provides methods for managing database structure. The methods can be grouped based on their responsibility: create, edit (alter or update), list or delete (drop) database elements. The following document lists the available methods, providing examples of their use. Introduction Every schema altering method in the Export module has an equivalent which returns the SQL that is used for the altering operation. For example createTable() executes the query / queries returned by createTableSql(). In this chapter the following tables will be created, altered and finally dropped, in a database named events_db: events Name Type Primary Auto Increment id integer true true name string(255) false false datetime timestamp false false people Name Type Primary Auto Increment ----------------- Brought to you by
Chapter 20: Database Abstraction Layer 293 id integer true true name string(255) false false event_participants Name Type Primary Auto Increment event_id integer true false person_id string(255) true false Creating Databases It is simple to create new databases with Doctrine. It is only a matter of calling the createDatabase() function with an argument that is the name of the database to create. // test.php Listing 20-1 // ... $conn->export->createDatabase('events_db'); Now lets change the connection in our bootstrap.php file to connect to the new events_db: // bootstrap.php Listing 20-2 /** * Bootstrap Doctrine.php, register autoloader and specify * configuration attributes */ // ... $conn = Doctrine_Manager::connection('mysql://root:@localhost/events_db', 'doctrine'); // ... Creating Tables Now that the database is created and we've re-configured our connection, we can proceed with adding some tables. The method createTable() takes three parameters: the table name, an array of field definition and some extra options (optional and RDBMS-specific). Now lets create the events table: // test.php Listing 20-3 // $definition = array( 'id' => array( 'type' => 'integer', 'primary' => true, 'autoincrement' => true ), 'name' => array( 'type' => 'string', 'length' => 255 ----------------- 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 10: Component Overview 175
Chapter 10: Component Overview 177
Chapter 10: Component Overview 179
Chapter 10: Component Overview 181
Chapter 10: Component Overview 183
Chapter 10: Component Overview 185
Chapter 10: Component Overview 187
Chapter 10: Component Overview 189
Chapter 10: Component Overview 191
Chapter 11: Native SQL 193 $users =
Chapter 12: YAML Schema Files 195 C
Chapter 12: YAML Schema Files 197 D
Chapter 12: YAML Schema Files 199 p
Chapter 12: YAML Schema Files 201 -
Chapter 12: YAML Schema Files 203 O
Chapter 12: YAML Schema Files 205 C
Chapter 12: YAML Schema Files 207 o
Chapter 13: Data Validation 209 Cha
Chapter 13: Data Validation 211 # .
Chapter 13: Data Validation 213 The
Chapter 13: Data Validation 215 if
Chapter 13: Data Validation 217 Her
Chapter 13: Data Validation 219 # .
Chapter 13: Data Validation 221 } e
Chapter 13: Data Validation 223 } }
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 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
Loading...
Loading...
Loading...
Magazine: Doctrine_manual-1-2-en