diff --git a/manual/docs/en/data-fixtures.txt b/manual/docs/en/data-fixtures.txt index 44952d3a1..4825268fd 100644 --- a/manual/docs/en/data-fixtures.txt +++ b/manual/docs/en/data-fixtures.txt @@ -51,30 +51,86 @@ $data->importDummyData($numRecords, $models); ++ Writing -You can write your fixtures files manually and load them in to your applications. Below is a sample data.yml fixtures file +You can write your fixtures files manually and load them in to your applications. Below is a sample data.yml fixtures file. You can also split your data fixtures file up in to multiple files. Doctrine will read all fixtures files and parse them, then load all data. + +Please see the [doc schema-files :index :name] for the sample models/schema for these example fixtures. --- -Email: - Email_1: - address: jwage@mac.com -Group: - Group_1: - name: Drama Actors -User: - User_1: - Phonenumber: Phonenumber_1 +Adult: + Adult_1: + name: Parent 1 + Contact: Contact_1 +Car: + Car_1: + name: Chevorlet Trailblazer + Car_2: + name: Chevorlet Blazer + Car_3: + name: Buick +Child: + Child_1: + name: Child 1 + Adult: Adult_1 +Contact: + Contact_1: name: Jonathan H. Wage - loginname: jwage - Email: Email_1 -Groupuser: - Groupuser_1: - Group: Group_1 + Contact_3: + name: Daniel Adams + Contact_4: + name: Robert Adams +Dog: + Dog_1: + name: Sam User: User_1 -Phonenumber: - Phonenumber_1: - phonenumber: 555-555-5555 - Group: Group_1 + Dog_2: + name: Dixie + User: User_2 + Dog_3: + name: Chief + User: User_3 +SelfReference: + SelfReference_1: + User1: User_1 + User2: User_2 + name: Self Reference 1 + SelfReference1: SelfReference_2 + SelfReference2: SelfReference_3 + SelfReference_2: + User1: User_2 + User2: User_1 + name: Self Reference 2 + SelfReference1: SelfReference_1 + SelfReference2: SelfReference_1 + SelfReference_3: + User1: User_2 + User2: User_1 + name: Self Reference 3 + SelfReference1: SelfReference_3 + SelfReference2: SelfReference_3 +User: + User_1: + username: jwage + hair_color: light brown + Contact: Contact_1 + User_2: + username: dadams + hair_color: dark brown + Contact: Contact_3 + User_3: + username: radams + hair_color: light brown + Contact: Contact_4 +UserCar: + UserCar_1: + User: User_1 + Car: Car_1 + UserCar_2: + User: User_2 + Car: Car_2 + UserCar_3: + User: User_3 + Car: Car_3 Here is how you would write code to load the data from that data.yml file diff --git a/manual/docs/en/schema-files.txt b/manual/docs/en/schema-files.txt index a40f3290d..1e0586765 100644 --- a/manual/docs/en/schema-files.txt +++ b/manual/docs/en/schema-files.txt @@ -2,50 +2,20 @@ 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. +Schema files support all the normal things you would write with manual php code. Component to connection binding, relationships, attributes, templates/behaviors, indexes, etc. + ++ Example Schema File -'tableName' and 'className' are optional. If not specified they will be set by the key of the yaml block +Below is an example schema file for generating a set of models. + +You will notice in this schema file it is not always necessary to specify the local and foreign parameters on a relationship. If the foreign columns follow the naming patterns, Doctrine can successfully guess each of them. schema.yml --- -User: - columns: - id: - notnull: true - primary: true - autoincrement: true - type: integer - length: 4 - name: id - username: - type: string - length: 255 - relations: - Groups: - class: Group - refClass: UserGroup - local: user_id - foreign: group_id - type: many -UserGroup: - columns: - user_id: - type: integer - length: 4 - primary: true - group_id: - type: integer - length: 4 - primary: true - relations: - User: - local: user_id - foreign: id - Group: - local: group_id - foreign: id -Group: +Group: +# bind this model to connection1 + connection: connection1 columns: id: notnull: true @@ -61,12 +31,203 @@ Group: Users: class: User refClass: UserGroup - local: group_id - foreign: user_id - type: many +# set attributes on the model + attributes: + export: tables +# you can set templates on your schema with the following syntax +# if you do not require any options to be set for the template +# actAs and templates are the same, actAs serves as a convenience method for the Templates/Plugins +# that come bundled with Doctrine + templates: [Doctrine_Template_NestedSet, Doctrine_Template_Versionable] +# this below syntax can be used for the templates above + actAs: + NestedSet: + hasManyRoots: true + rootColumnName: root_id +UserGroup: + columns: + user_id: + type: integer + length: 4 + primary: true + group_id: + type: integer + length: 4 + primary: true + relations: + User: - + Group: - +UserCar: + columns: + user_id: + type: integer + length: 11 + primary: true + car_id: + type: integer + length: 11 + primary: true + relations: + User: - + Car: - +Adult: + fields: + id: + type: integer + size: 11 + primary: true + autoincrement: true + name: + type: string + size: 255 + contact_id: + type: integer + size: 11 + relations: + Contact: + foreignType: one +Car: + columns: + id: + type: integer + length: 11 + primary: true + autoincrement: true + name: + type: string + length: 255 + relations: + Users: + class: User + refClass: UserCar +Child: + fields: + id: + type: integer + size: 11 + primary: true + autoincrement: true + name: + type: string + size: 255 + adult_id: + type: integer + size: 11 + relations: + Adult: + foreignAlias: Children +Contact: + columns: + id: + type: integer + length: 11 + primary: true + autoincrement: true + name: + type: string + length: 255 +Dog: + columns: + id: + type: integer + length: 11 + primary: true + autoincrement: true + name: + type: string + length: 255 + user_id: + type: integer + length: 11 + relations: + User: + foreignType: one +SelfReference: + fields: + id: + type: integer + size: 11 + primary: true + autoincrement: true + name: + type: string + size: 255 + user_id1: + type: integer + size: 11 + user_id2: + type: integer + size: 11 + parent_self_reference_id: + type: integer + size: 11 + parent_self_reference_id2: + type: integer + size: 11 + relations: + User1: + class: User + local: user_id1 + foreignAlias: SelfReference1 + User2: + class: User + local: user_id2 + foreignAlias: SelfReference2 + SelfReference1: + class: SelfReference + local: parent_self_reference_id + foreignAlias: SelfReferences1 + SelfReference2: + class: SelfReference + local: parent_self_reference_id2 + foreignAlias: SelfReferences2 +User: + inheritance: + extends: Entity + fields: + id: + type: integer + size: 11 + primary: true + autoincrement: true + username: + type: string + length: 255 + hair_color: + type: string + length: 255 + contact_id: + type: integer + length: 11 + relations: + Contact: + local: contact_id + foreign: id + foreignType: one + Cars: + class: Car + refClass: UserCar + Groups: + class: Group + refClass: UserGroup + indexes: + name_x: + columns: + username: + sorting: ASC + length: 11 + primary: true + type: unique +Entity: + columns: + id: + type: integer + size: 11 + primary: true + autoincrement: true -And now we want to use some Doctrine code to parse that schema yml file and generate our models from it +And now we want to use some Doctrine code to parse that schema.yml file and generate our models from it. // This code will generate the models for schema.yml at /path/to/generate/models @@ -74,124 +235,34 @@ $import = new Doctrine_Import_Schema(); $import->importSchema('schema.yml', 'yml', '/path/to/generate/models'); -This is the directory structure that would be generated at /path/to/generate/models +This is the directory structure that would be generated at /path/to/generate/models. The base classes contain the actual definitions for the model, and the top level models extend the base and they are only written the first time so you are able to modify them without your additions being overwritten. +- Adult.class.php +- Car.class.php +- Child.class.php +- Contact.class.php +- Dog.class.php +- Entity.class.php - Group.class.php +- SelfReference.class.php - User.class.php +- UserCar.class.php - UserGroup.class.php - generated + - BaseAdult.class.php + - BaseCar.class.php + - BaseChild.class.php + - BaseContact.class.php + - BaseDog.class.php + - BaseEntity.class.php - BaseGroup.class.php + - BaseSelfReference.class.php - BaseUser.class.php + - BaseUserCar.class.php - BaseUserGroup.class.php -And finally here is the code for each of the generated models - - - -// Group.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -class Group extends BaseGroup -{ -} - -// User.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -class User extends BaseUser -{ -} - -// UserGroup.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -class UserGroup extends BaseUserGroup -{ -} - -// BaseGroup.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -abstract class BaseGroup extends sfDoctrineRecord -{ - - public function setTableDefinition() - { - $this->setTableName('group'); - $this->hasColumn('id', 'integer', 4, array('notnull' => true, - 'primary' => true, - 'autoincrement' => true)); - $this->hasColumn('name', 'string', 255); - } - - public function setUp() - { - $this->hasMany('User as Users', array('refClass' => 'UserGroup', - 'local' => 'group_id', - 'foreign' => 'user_id')); - } -} - -// BaseUser.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -abstract class BaseUser extends sfDoctrineRecord -{ - - public function setTableDefinition() - { - $this->setTableName('user_table'); - $this->hasColumn('id', 'integer', 4, array('notnull' => true, - 'primary' => true, - 'autoincrement' => true)); - $this->hasColumn('username', 'string', 255); - } - - public function setUp() - { - $this->hasMany('Group as Groups', array('refClass' => 'UserGroup', - 'local' => 'user_id', - 'foreign' => 'group_id')); - } -} - -// BaseUserGroup.class.php - -/** - * This class has been auto-generated by the Doctrine ORM Framework - */ -abstract class BaseUserGroup extends sfDoctrineRecord -{ - - public function setTableDefinition() - { - $this->setTableName('user_group'); - $this->hasColumn('user_id', 'integer', 4, array('primary' => true)); - $this->hasColumn('group_id', 'integer', 4, array('primary' => true)); - } - - public function setUp() - { - $this->hasOne('User', array('local' => 'user_id', - 'foreign' => 'id')); - $this->hasOne('Group', array('local' => 'group_id', - 'foreign' => 'id')); - } -} - - ++ Indexes Please see chapter [doc basic-schema-mapping :index :name] for more information about indexes and their options. @@ -200,11 +271,18 @@ schema.yml --- UserProfile: - tableName: user_profile columns: - user_id: { type: integer, length: 4, primary: true, autoincrement: true } - first_name: { type: string, length: 20 } - last_name: { type: string, length: 20 } + 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: diff --git a/sandbox/models/Contact.class.php b/sandbox/models/Contact.class.php index b646b7f21..2531982f8 100644 --- a/sandbox/models/Contact.class.php +++ b/sandbox/models/Contact.class.php @@ -1,19 +1,12 @@ setTableName('contact'); - $this->hasColumn('name', 'string', 255); + $this->setTableName('contact'); + $this->hasColumn('id', 'integer', 11, array('primary' => true, + 'autoincrement' => true)); + $this->hasColumn('name', 'string', 255); + + + + + } public function setUp() { - $this->hasMany('User', array('local' => 'id', - 'foreign' => 'contact_id')); + $this->hasOne('Adult', array('local' => 'id', + 'foreign' => 'contact_id')); + + $this->hasOne('User', array('local' => 'id', + 'foreign' => 'contact_id')); + } } \ No newline at end of file diff --git a/sandbox/models/generated/BaseUser.class.php b/sandbox/models/generated/BaseUser.class.php index a2a74df32..a962dc96e 100644 --- a/sandbox/models/generated/BaseUser.class.php +++ b/sandbox/models/generated/BaseUser.class.php @@ -3,20 +3,49 @@ /** * This class has been auto-generated by the Doctrine ORM Framework */ -abstract class BaseUser extends Doctrine_Record +abstract class BaseUser extends Entity { public function setTableDefinition() { - $this->setTableName('user'); - $this->hasColumn('username', 'string', 255); - $this->hasColumn('contact_id', 'integer', 11); + $this->setTableName('user'); + $this->hasColumn('id', 'integer', 11, array('primary' => true, + 'autoincrement' => true)); + + $this->hasColumn('username', 'string', 255); + + $this->hasColumn('hair_color', 'string', 255); + $this->hasColumn('contact_id', 'integer', 11); + + $this->index('name_x', array('fields' => array('username' => array( 'sorting' => 'ASC', 'length' => '11', 'primary' => true, ), ), 'type' => 'unique')); + + + + } public function setUp() { - $this->hasOne('Contact', array('local' => 'contact_id', - 'foreign' => 'id')); + $this->hasOne('Contact', array('local' => 'contact_id', + 'foreign' => 'id')); + + $this->hasMany('Car as Cars', array('refClass' => 'UserCar', + 'local' => 'user_id', + 'foreign' => 'car_id')); + + $this->hasMany('Group as Groups', array('refClass' => 'UserGroup', + 'local' => 'user_id', + 'foreign' => 'group_id')); + + $this->hasOne('Dog', array('local' => 'id', + 'foreign' => 'user_id')); + + $this->hasMany('SelfReference as SelfReference1', array('local' => 'id', + 'foreign' => 'user_id1')); + + $this->hasMany('SelfReference as SelfReference2', array('local' => 'id', + 'foreign' => 'user_id2')); + } } \ No newline at end of file diff --git a/sandbox/schema/contact.yml b/sandbox/schema/contact.yml index d1fa3ab41..3fadb71ae 100644 --- a/sandbox/schema/contact.yml +++ b/sandbox/schema/contact.yml @@ -1,6 +1,11 @@ --- Contact: columns: + id: + type: integer + length: 11 + primary: true + autoincrement: true name: type: string length: 255 \ No newline at end of file diff --git a/sandbox/schema/user.yml b/sandbox/schema/user.yml index 895596450..43dde72c9 100644 --- a/sandbox/schema/user.yml +++ b/sandbox/schema/user.yml @@ -1,13 +1,38 @@ --- User: - columns: + inheritance: + extends: Entity + fields: + id: + type: integer + size: 11 + primary: true + autoincrement: true username: type: string length: 255 + hair_color: + type: string + length: 255 contact_id: type: integer length: 11 relations: Contact: local: contact_id - foreign: id \ No newline at end of file + foreign: id + foreignType: one + Cars: + class: Car + refClass: UserCar + Groups: + class: Group + refClass: UserGroup + indexes: + name_x: + columns: + username: + sorting: ASC + length: 11 + primary: true + type: unique \ No newline at end of file