| 13 | | the foreign key exists, although both ways are supported. |
| | 85 | the foreign key exists. When the schema file is parsed, it inflects the relationship and builds the |
| | 86 | opposite end automatically. If you specify the other end of the relationship manually, the auto |
| | 87 | generation will have no effect. |
| | 88 | |
| | 89 | Doctrine will also attempt to guess the local and foreign key names based on the class involved in the |
| | 90 | relationship. You will notice that all of our example schemas do not include the specific local and |
| | 91 | foreign keys because it is able to guess them. |
| | 92 | |
| | 93 | +++ Detect Relations |
| | 94 | |
| | 95 | Doctrine offers the ability to specify a detect_relations options. This feature provides automatic |
| | 96 | relationship building based on column names. If you have a User model with a contact_id and a class |
| | 97 | with the name Contact exists, it will automatically create the relationships between the two. |
| | 98 | |
| | 99 | +++ Customizing Relationships |
| | 100 | |
| | 101 | Doctrine only requires that you specify the relationship on the end where the foreign key exists. The |
| | 102 | opposite end of the relationship will be inflected and built on the opposite end. The schema syntax |
| | 103 | offers the ability to customize the relationship alias and type of the opposite end. This is good news |
| | 104 | because it means you can maintain all the relevant relationship information in one place. Below is an |
| | 105 | example of how to customize the alias and type of the opposite end of the relationship. It demonstrates |
| | 106 | the relationships User hasOne Contact and Contact hasOne User as UserModel. Normally it would have |
| | 107 | automatically generated User hasOne Contact and Contact hasMany User. The foreignType and foreignAlias |
| | 108 | keywords allow you to customize the foreign relationship. |
| | 109 | |
| | 110 | <code type="yaml"> |
| | 111 | --- |
| | 112 | User: |
| | 113 | columns: |
| | 114 | id: |
| | 115 | type: integer(4) |
| | 116 | primary: true |
| | 117 | autoincrement: true |
| | 118 | contact_id: |
| | 119 | type: integer(4) |
| | 120 | username: |
| | 121 | type: string(255) |
| | 122 | password: |
| | 123 | type: string(255) |
| | 124 | relations: |
| | 125 | Contact: |
| | 126 | foreignType: one |
| | 127 | foreignAlias: UserModel |
| | 128 | |
| | 129 | Contact: |
| | 130 | columns: |
| | 131 | id: |
| | 132 | type: integer(4) |
| | 133 | primary: true |
| | 134 | autoincrement: true |
| | 135 | name: |
| | 136 | type: string(255) |
| | 137 | </code> |
| | 138 | |
| | 139 | Here is an example schema: |
| | 140 | |
| | 141 | <code type="yaml"> |
| | 142 | --- |
| | 143 | detect_relations: true |
| | 144 | |
| | 145 | User: |
| | 146 | columns: |
| | 147 | id: |
| | 148 | type: integer(4) |
| | 149 | primary: true |
| | 150 | autoincrement: true |
| | 151 | contact_id: |
| | 152 | type: integer(4) |
| | 153 | username: |
| | 154 | type: string(255) |
| | 155 | password: |
| | 156 | type: string(255) |
| | 157 | |
| | 158 | Contact: |
| | 159 | columns: |
| | 160 | id: |
| | 161 | type: integer(4) |
| | 162 | primary: true |
| | 163 | autoincrement: true |
| | 164 | name: |
| | 165 | type: string(255) |
| | 166 | </code> |
| | 167 | |
| | 168 | The resulting relationships will be User hasOne Contact and Contact hasMany User. |
| 310 | | ++ Generating Models |
| | 467 | +++ Generate Accessors |
| | 468 | |
| | 469 | When the generate_accessors: true option is present in a schema file, it will generate propel orm style |
| | 470 | get and set accessors in the Base model definition. For example setFieldName() and getFieldName() would |
| | 471 | then be possible in your models. |
| | 472 | |
| | 473 | Example: |
| | 474 | <code type="yaml"> |
| | 475 | --- |
| | 476 | generate_accessors: true |
| | 477 | |
| | 478 | User: |
| | 479 | columns: |
| | 480 | username: |
| | 481 | type: string(255) |
| | 482 | </code> |
| | 483 | |
| | 484 | +++ Column Aliases |
| | 485 | |
| | 486 | If you want the ability alias a column name as something other than the column name in the database |
| | 487 | this is possible with the " as alias_name" string after the column name. |
| | 488 | |
| | 489 | Example: |
| | 490 | <code type="yaml"> |
| | 491 | --- |
| | 492 | User: |
| | 493 | columns: |
| | 494 | login: |
| | 495 | name: login as username |
| | 496 | type: string(255) |
| | 497 | password: |
| | 498 | type: string(255) |
| | 499 | </code> |
| | 500 | |
| | 501 | The above example would allow you to access the login column name from the alias "username". |
| | 502 | |
| | 503 | +++ Packages |
| | 504 | |
| | 505 | Doctrine offers the "package" parameter which will generate the models in to sub folders. With large |
| | 506 | schema files this will allow you to better organize your schemas in to folders. |
| | 507 | |
| | 508 | <code type="yaml"> |
| | 509 | --- |
| | 510 | User: |
| | 511 | package: User |
| | 512 | columns: |
| | 513 | username: string(255) |
| | 514 | </code> |
| | 515 | |
| | 516 | The model files from this schema file would be put in a folder named User. You can specify more sub |
| | 517 | folders by doing "package: User.Models" and the models would be in User/Models |
| | 518 | |
| | 519 | +++ Global Schema Information |
| | 520 | |
| | 521 | Doctrine schemas allow you to specify certain parameters that will apply to all of the models defined |
| | 522 | in the schema file. Below you can find an example on what global parameters you can set for schema files. |
| | 523 | |
| | 524 | List of global parameters: |
| | 525 | |
| | 526 | connection |
| | 527 | attributes |
| | 528 | templates |
| | 529 | actAs |
| | 530 | options |
| | 531 | package |
| | 532 | inheritance |
| | 533 | detect_relations |
| | 534 | generate_accessors |
| | 535 | |
| | 536 | <code type="yaml"> |
| | 537 | --- |
| | 538 | connection: conn_name1 |
| | 539 | actAs: [Timestampable] |
| | 540 | options: |
| | 541 | type: INNODB |
| | 542 | package: User |
| | 543 | detect_relations: true |
| | 544 | generate_accessors: true |
| | 545 | |
| | 546 | User: |
| | 547 | columns: |
| | 548 | id: |
| | 549 | type: integer(4) |
| | 550 | primary: true |
| | 551 | autoincrement: true |
| | 552 | contact_id: |
| | 553 | type: integer(4) |
| | 554 | username: |
| | 555 | type: string(255) |
| | 556 | password: |
| | 557 | type: string(255) |
| | 558 | |
| | 559 | Contact: |
| | 560 | columns: |
| | 561 | id: |
| | 562 | type: integer(4) |
| | 563 | primary: true |
| | 564 | autoincrement: true |
| | 565 | name: |
| | 566 | type: string(255) |
| | 567 | </code> |
| | 568 | |
| | 569 | All of the settings at the top will be applied to every model which is defined in that yaml file. |
| | 570 | |
| | 571 | ++ Using Schema Files |