Commit 90fd4229 authored by Jonathan.Wage's avatar Jonathan.Wage

Updates to sandbox and manual.

parent 1bbc5b15
......@@ -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.
<code type="yml">
---
Email:
Email_1:
address: jwage@mac.com
Group:
Group_1:
name: Drama Actors
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
Contact_3:
name: Daniel Adams
Contact_4:
name: Robert Adams
Dog:
Dog_1:
name: Sam
User: User_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:
Phonenumber: Phonenumber_1
name: Jonathan H. Wage
loginname: jwage
Email: Email_1
Groupuser:
Groupuser_1:
Group: Group_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
Phonenumber:
Phonenumber_1:
phonenumber: 555-555-5555
Group: Group_1
Car: Car_1
UserCar_2:
User: User_2
Car: Car_2
UserCar_3:
User: User_3
Car: Car_3
</code>
Here is how you would write code to load the data from that data.yml file
......
......@@ -2,14 +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
<code type="yml">
---
User:
Group:
# bind this model to connection1
connection: connection1
columns:
id:
notnull: true
......@@ -18,16 +24,26 @@ User:
type: integer
length: 4
name: id
username:
name:
type: string
length: 255
relations:
Groups:
class: Group
Users:
class: User
refClass: UserGroup
local: user_id
foreign: group_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:
......@@ -39,34 +55,179 @@ UserGroup:
length: 4
primary: true
relations:
User:
local: user_id
foreign: id
Group:
local: group_id
foreign: id
Group:
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:
notnull: true
type: integer
size: 11
primary: true
autoincrement: true
name:
type: string
size: 255
contact_id:
type: integer
length: 4
name: id
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
local: group_id
foreign: user_id
type: many
indexes:
name_x:
columns:
username:
sorting: ASC
length: 11
primary: true
type: unique
Entity:
columns:
id:
type: integer
size: 11
primary: true
autoincrement: true
</code>
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.
<code type="php">
// 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');
</code>
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.
<code>
- 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
</code>
And finally here is the code for each of the generated models
<code type="php">
// 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'));
}
}
</code>
++ Indexes
Please see chapter [doc basic-schema-mapping :index :name] for more information about indexes and their options.
......@@ -200,11 +271,18 @@ schema.yml
<code type="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:
......
<?php
require_once('generated/BaseContact.class.php');
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class Contact extends BaseContact
{
public function setTableDefinition()
{
parent::setTableDefinition();
}
public function setUp()
{
parent::setUp();
}
}
\ No newline at end of file
<?php
require_once('generated/BaseUser.class.php');
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
......@@ -7,4 +8,5 @@ class User extends BaseUser
{
}
\ No newline at end of file
......@@ -9,13 +9,24 @@ abstract class BaseContact extends Doctrine_Record
public function setTableDefinition()
{
$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',
$this->hasOne('Adult', array('local' => 'id',
'foreign' => 'contact_id'));
$this->hasOne('User', array('local' => 'id',
'foreign' => 'contact_id'));
}
}
\ No newline at end of file
......@@ -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('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->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
---
Contact:
columns:
id:
type: integer
length: 11
primary: true
autoincrement: true
name:
type: string
length: 255
\ No newline at end of file
---
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
......@@ -11,3 +21,18 @@ User:
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
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment