1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/manual/docs/en/schema-files.txt

300 lines
6.4 KiB
Plaintext
Raw Normal View History

2007-10-06 00:19:54 +04:00
++ Introduction
2007-11-11 03:51:13 +03:00
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.
2007-10-06 00:19:54 +04:00
2007-10-18 08:03:33 +04:00
Schema files support all the normal things you would write with manual php code. Component to connection binding, relationships, attributes, templates/behaviors, indexes, etc.
2007-11-11 03:51:13 +03:00
++ Relationships
2007-10-06 00:19:54 +04:00
2007-11-11 04:20:45 +03:00
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:
type: string(255)
2007-11-11 04:20:45 +03:00
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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
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:
2007-11-16 20:40:29 +03:00
tableName: group_table
2007-11-11 04:20:45 +03:00
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>
2007-11-11 03:51:13 +03:00
++ Connection Binding
2007-10-18 08:03:33 +04:00
2007-11-11 04:20:45 +03:00
<code type="php">
Doctrine::connection('mysql://jwage:pass@localhost/connection1', 'connection1');
</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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
</code>
2007-11-11 03:51:13 +03:00
++ Attributes
2007-10-06 00:19:54 +04:00
2007-11-11 04:20:45 +03:00
<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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
attributes:
export: none
validate: false
</code>
2007-10-06 00:19:54 +04:00
2007-11-11 03:51:13 +03:00
++ Templates
2007-10-06 00:19:54 +04:00
2007-11-11 04:20:45 +03:00
<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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
templates:
MyCustomTemplate
option1: value
option2: value
</code>
2007-11-11 03:51:13 +03:00
++ ActAs
2007-10-06 00:19:54 +04:00
2007-11-11 04:20:45 +03:00
<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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
actAs:
Sluggable:
fields: [username]
</code>
2007-11-11 03:51:13 +03:00
++ Options
2007-10-06 00:19:54 +04:00
2007-11-11 04:20:45 +03:00
<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:
2007-12-10 16:14:02 +03:00
type: string(255)
2007-11-11 04:20:45 +03:00
options:
type: INNODB
collate: utf8_unicode_ci
charset: utf8
</code>
++ 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:
2007-10-18 08:03:33 +04:00
user_id:
type: integer
length: 4
primary: true
autoincrement: true
first_name:
type: string
length: 20
last_name:
type: string
length: 20
indexes:
name_index:
fields:
first_name:
sorting: ASC
length: 10
primary: true
2007-11-14 04:57:39 +03:00
last_name: []
type: unique
</code>
This is the PHP line of code that is auto-generated inside setTableDefinition() inside your base model class.
<code type="php">
2007-11-11 03:51:13 +03:00
$this->index('name_index', array('fields' => array('first_name' => array( 'sorting' => 'ASC', 'length' => '10', 'primary' => true ), 'last_name' => array( ) ), 'type' => 'unique'));
</code>
2007-11-14 05:12:52 +03:00
++ 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>
2007-11-11 03:51:13 +03:00
++ Generating Models
2007-11-11 03:51:13 +03:00
Once you have defined your schema files you need some code to
<code type="php">
2007-11-11 03:51:13 +03:00
// 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
2007-11-11 03:51:13 +03:00
// This code will generate the models for schema.yml at /path/to/generate/models
Doctrine::generateModelsFromYaml('schema.yml', '/path/to/generate/models', $options);
2007-11-14 04:57:39 +03:00
</code>