schema-files.txt 7.09 KB
Newer Older
1 2
++ Introduction

Jonathan.Wage's avatar
Jonathan.Wage committed
3
The purpose of schema files is to allow you to manage your model definitions directly from a yaml file rather then editing php code. The yaml schema file is parsed and used to generate all your model definitions/classes. This makes Doctrine model definitions much more portable.
4

5 6
Schema files support all the normal things you would write with manual php code. Component to connection binding, relationships, attributes, templates/behaviors, indexes, etc.

Jonathan.Wage's avatar
Jonathan.Wage committed
7
++ Relationships
8

Jonathan.Wage's avatar
Jonathan.Wage committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
When specifying relationships it is only necessary to specify the relationship on the end where the foreign key exists, although both ways are supported.

+++ One to One

<code type="yaml">
---
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
26
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  relations:
    Contact:
      foreignType: one
Contact:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
</code>

+++ One to Many

<code type="yaml">
---
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
55
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
Phonenumber:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
    user_id:
      type: integer(4)
  relations:
    User:
      foreignAlias: Phonenumbers
</code>

+++ Many to Many

<code type="yaml">
User:
  columns:
    id:
      type: integer(4)
      autoincrement: true
      primary: true
    username:
      type: string(255)
    password:
      type: string(255)
  attributes:
    export: all
    validate: true
Group:
Jonathan.Wage's avatar
Jonathan.Wage committed
88
  tableName: group_table
Jonathan.Wage's avatar
Jonathan.Wage committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  columns:
    id:
      type: integer(4)
      autoincrement: true
      primary: true
    name:
      type: string(255)
  relations:
    Users:
      foreignAlias: Groups
      class: User
      refClass: GroupUser
GroupUser:
  columns:
    group_id:
      type: integer(4)
      primary: true
    user_id:
      type: integer(4)
      primary: true
</code>

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
+++ One to One Aggregate

<code type="yaml">
---
User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
      type: string(255)
  relations:
    Contact:
      foreignType: one
      onDelete: CASCADE
Contact:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
141
++ Connection Binding
142

Jonathan.Wage's avatar
Jonathan.Wage committed
143
<code type="php">
144
Doctrine::connection('mysql://user:pass@localhost/connection1', 'connection1');
Jonathan.Wage's avatar
Jonathan.Wage committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
</code>

<code type="yaml">
---
User:
  connection: connection1
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
161
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
162 163
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
164
++ Attributes
165

Jonathan.Wage's avatar
Jonathan.Wage committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
<code type="yaml">
---
User:
  connection: connection1
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
180
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
181 182 183 184
  attributes:
    export: none
    validate: false
</code>
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
++ Enums

<code type="yaml">
---
TvListing:
 tableName: tv_listing
 actAs: [Timestampable]
 columns:
   notes:
     type: string
   taping:
     type: enum
     length: 4
     values: ['live', 'tape']
   region:
     type: enum
     length: 4
     values: ['US', 'CA']
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
206
++ Templates
207

Jonathan.Wage's avatar
Jonathan.Wage committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221
<code type="yaml">
---
User:
  connection: connection1
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
222
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
223 224 225 226 227 228
  templates:
    MyCustomTemplate
      option1: value
      option2: value
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
229
++ ActAs
230

Jonathan.Wage's avatar
Jonathan.Wage committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244
<code type="yaml">
---
User:
  connection: connection1
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
245
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
246 247 248 249 250
  actAs:
    Sluggable:
      fields: [username]
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
251
++ Options
252

Jonathan.Wage's avatar
Jonathan.Wage committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266
<code type="yaml">
---
User:
  connection: connection1
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
pookey's avatar
pookey committed
267
      type: string(255)
Jonathan.Wage's avatar
Jonathan.Wage committed
268 269 270 271 272 273
  options:
    type: INNODB
    collate: utf8_unicode_ci
    charset: utf8
</code>

274 275 276 277 278 279 280 281 282
++ Indexes

Please see chapter [doc basic-schema-mapping :index :name] for more information about indexes and their options.

schema.yml
<code type="yml">
---
UserProfile:
  columns:
283 284 285 286 287 288 289 290 291 292 293
    user_id:
      type: integer
      length: 4
      primary: true
      autoincrement: true
    first_name:
      type: string
      length: 20
    last_name:
      type: string
      length: 20
294 295 296 297 298 299 300
  indexes:
    name_index:
      fields:
        first_name:
          sorting: ASC
          length: 10
          primary: true
Jonathan.Wage's avatar
Jonathan.Wage committed
301
        last_name: []
302 303 304 305 306 307
      type: unique
</code>

This is the PHP line of code that is auto-generated inside setTableDefinition() inside your base model class.

<code type="php">
Jonathan.Wage's avatar
Jonathan.Wage committed
308
$this->index('name_index', array('fields' => array('first_name' => array( 'sorting' => 'ASC', 'length' => '10', 'primary' => true ), 'last_name' => array( ) ), 'type' => 'unique'));
309 310
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
++ Inheritance

<code type="yaml">
---
Entity:
  actAs: [Timestampable]
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(255)
    password:
      type: string(255)
User:
  inheritance:
    extends: Entity
Group:
  inheritance:
    extends: Entity
</code>

Jonathan.Wage's avatar
Jonathan.Wage committed
334
++ Generating Models
335

Jonathan.Wage's avatar
Jonathan.Wage committed
336
Once you have defined your schema files you need some code to 
337
<code type="php">
Jonathan.Wage's avatar
Jonathan.Wage committed
338 339 340 341 342 343 344 345
// The options are completely optional. Only use this if you need something beyond the default configuration for model generation
$options = array('packagesPrefix'        =>  'Package',             // What to prefix the middle package models with
                 'packagesPath'          =>  '',                    // this defaults to the "#models_path#/packages"
                 'generateBaseClasses'   =>  true,                  // Whether or not to generate abstract base models containing the definition and a top level class which is empty extends the base
                 'generateTableClasses'  =>  true,                  // Whether or not to generate a table class for each model
                 'baseClassesDirectory'  =>  'generated',           // Name of the folder to generate the base class definitions in
                 'baseClassName'         =>  'Doctrine_Record',     // Name of the base Doctrine_Record class
                 'suffix'                =>  '.php');               // Extension for your generated models
346

Jonathan.Wage's avatar
Jonathan.Wage committed
347 348
// This code will generate the models for schema.yml at /path/to/generate/models
Doctrine::generateModelsFromYaml('schema.yml', '/path/to/generate/models', $options);
Jonathan.Wage's avatar
Jonathan.Wage committed
349
</code>