Commit ed383556 authored by Jonathan.Wage's avatar Jonathan.Wage

Merged r3136:3137

parent b889f427
......@@ -441,9 +441,17 @@ final class Doctrine
*
* Array of all the loaded models and the path to each one for autoloading
*
* @var string
* @var array
*/
private static $_loadedModels = array();
/**
* _validators
*
* Array of all the loaded validators
* @var array
*/
private static $_validators = array();
/**
* __construct
......@@ -1070,7 +1078,13 @@ final class Doctrine
return mkdir($path, $mode, true);
}
function removeDirectories($folderPath)
/**
* removeDirectories
*
* @param string $folderPath
* @return void
*/
public static function removeDirectories($folderPath)
{
if (is_dir($folderPath))
{
......@@ -1093,4 +1107,31 @@ final class Doctrine
return false;
}
}
/**
* getValidators
*
* Get available doctrine validators
*
* @return array $validators
*/
public static function getValidators()
{
if (empty(self::$_validators)) {
$dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Validator';
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $file) {
$e = explode('.', $file->getFileName());
if (end($e) == 'php') {
$name = strtolower($e[0]);
self::$_validators[$name] = $name;
}
}
}
return self::$_validators;
}
}
\ No newline at end of file
......@@ -329,7 +329,7 @@ END;
* @param string $table
* @param array $tableColumns
*/
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes, array $templates, array $actAs)
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes, array $templates, array $actAs, array $tableOptions)
{
$ret = array();
......@@ -355,36 +355,16 @@ END;
$ret[$i] .= ', null';
}
$a = array();
if (isset($column['default'])) {
$a[] = '\'default\' => ' . var_export($column['default'], true);
}
if (isset($column['notnull']) && $column['notnull']) {
$a[] = '\'notnull\' => true';
}
if (isset($column['primary']) && $column['primary']) {
$a[] = '\'primary\' => true';
}
if ((isset($column['autoinc']) && $column['autoinc']) || isset($column['autoincrement']) && $column['autoincrement']) {
$a[] = '\'autoincrement\' => true';
}
if (isset($column['unique']) && $column['unique']) {
$a[] = '\'unique\' => true';
}
if (isset($column['unsigned']) && $column['unsigned']) {
$a[] = '\'unsigned\' => true';
}
if ($column['type'] == 'enum' && isset($column['values']) ) {
$a[] = '\'values\' => array(\'' . implode('\',\'', $column['values']) . '\')';
}
if ( ! empty($a)) {
$ret[$i] .= ', ' . 'array(';
$length = strlen($ret[$i]);
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
$options = $column;
$unset = array('name', 'type', 'length', 'ptype');
foreach ($options as $key => $value) {
if (in_array($key, $unset) || $value === null) {
unset($options[$key]);
}
}
$ret[$i] .= ', ' . var_export($options, true);
$ret[$i] .= ');';
if ($i < (count($columns) - 1)) {
......@@ -403,6 +383,9 @@ END;
$i++;
$ret[$i] = $this->buildActAs($actAs);
$i++;
$ret[$i] = $this->buildTableOptions($tableOptions);
$code = implode("\n", $ret);
$code = trim($code);
......@@ -424,7 +407,7 @@ END;
foreach ($templates as $name => $options) {
if (is_array($options) && !empty($options)) {
$optionsPhp = $this->arrayToPhpArrayCode($options);
$optionsPhp = var_export($options, true);
$build .= " \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");\n";
} else {
......@@ -450,7 +433,7 @@ END;
$build = '';
foreach ($actAs as $name => $options) {
if (is_array($options) && !empty($options)) {
$optionsPhp = $this->arrayToPhp($options);
$optionsPhp = var_export($options, true);
$build .= " \$this->actAs('" . $name . "', " . $optionsPhp . ");\n";
} else {
......@@ -465,22 +448,6 @@ END;
return $build;
}
/**
* arrayToPhp
*
* @param string $array
* @return void
*/
protected function arrayToPhp(array $array)
{
ob_start();
var_export($array);
$php = ob_get_contents();
ob_end_clean();
return $php;
}
/**
* buildAttributes
*
......@@ -514,6 +481,22 @@ END;
return $build;
}
/**
* buildTableOptions
*
* @param string $array
* @return void
*/
public function buildTableOptions(array $options)
{
$build = '';
foreach ($options as $name => $value) {
$build .= " \$this->option('$name', " . var_export($value, true) . ");\n";
}
return $build;
}
/**
* buildIndexes
......@@ -526,56 +509,9 @@ END;
$build = '';
foreach ($indexes as $indexName => $definitions) {
$build .= "\n ".'$this->index(\'' . $indexName . '\', array(';
foreach ($definitions as $name => $value) {
// parse fields
if ($name === 'fields' || $name === 'columns') {
$build .= '\'fields\' => array(';
foreach ($value as $fieldName => $fieldValue) {
$build .= '\'' . $fieldName . '\' => array( ';
// parse options { sorting, length, primary }
if (isset($fieldValue) && $fieldValue) {
foreach ($fieldValue as $optionName => $optionValue) {
$build .= '\'' . $optionName . '\' => ';
// check primary option, mark either as true or false
if ($optionName === 'primary') {
$build .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
continue;
}
// convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') {
$build .= '\'' . strtoupper($optionValue) . '\', ';
continue;
}
// check the rest of the options
$build .= '\'' . $optionValue . '\', ';
}
}
$build .= '), ';
}
}
// parse index type option, 4 choices { unique, fulltext, gist, gin }
if ($name === 'type') {
$build .= '), \'type\' => \'' . $value . '\'';
}
// add extra ) if type definition is not declared
if ( ! isset($definitions['type'])) {
$build .= ')';
}
}
$build .= '));';
$build .= "\n \$this->index('" . $indexName . "'";
$build .= ', ' . var_export($definitions, true);
$build .= ');';
}
return $build;
......@@ -679,7 +615,7 @@ END;
* @param array $actAs
* @return string
*/
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array(), array $templates = array(), array $actAs = array())
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
{
if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -690,7 +626,7 @@ END;
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:$this->_baseClassName;
if ( ! (isset($options['no_definition']) && $options['no_definition'] === true)) {
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
$setUp = $this->buildSetUp($options, $columns, $relations);
} else {
$definition = null;
......@@ -721,7 +657,7 @@ END;
* @param array $actAs
* @return void=
*/
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array())
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
{
if ( !isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -768,7 +704,7 @@ END;
$baseClass['override_parent'] = true;
$baseClass['is_base_class'] = true;
$this->writeDefinition($baseClass, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$this->writeDefinition($baseClass, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
if (!empty($packageLevel)) {
$this->writeDefinition($packageLevel);
......@@ -776,7 +712,7 @@ END;
$this->writeDefinition($topLevel);
} else {
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
}
}
......@@ -819,9 +755,9 @@ END;
* @param array $actAs
* @return void
*/
public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array())
public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array(), array $tableOptions = array())
{
$definition = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$definition = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
$fileName = $options['className'] . $this->_suffix;
......@@ -903,4 +839,4 @@ END;
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath);
}
}
}
}
\ No newline at end of file
......@@ -160,8 +160,9 @@ class Doctrine_Import_Schema
$attributes = $this->getAttributes($properties);
$templates = $this->getTemplates($properties);
$actAs = $this->getActAs($properties);
$tableOptions = $this->getTableOptions($properties);
$builder->buildRecord($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$builder->buildRecord($options, $columns, $relations, $indexes, $attributes, $templates, $actAs, $tableOptions);
}
}
......@@ -282,6 +283,17 @@ class Doctrine_Import_Schema
{
return isset($properties['actAs']) ? $properties['actAs']:array();
}
/**
* getTableOptions
*
* @param string $properties
* @return void
*/
public function getTableOptions($properties)
{
return isset($properties['options']) ? $properties['options']:array();
}
/**
* parseSchema
......@@ -332,17 +344,21 @@ class Doctrine_Import_Schema
}
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
$colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null;
$colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
$colDesc['default'] = isset($field['default']) ? $field['default']:null;
$colDesc['notnull'] = isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null;
$colDesc['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null;
$colDesc['autoincrement'] = isset($field['autoinc']) ? (bool) (isset($field['autoinc']) && $field['autoinc']):$colDesc['autoincrement'];
$colDesc['unique'] = isset($field['unique']) ? (bool) (isset($field['unique']) && $field['unique']):null;
$colDesc['values'] = isset($field['values']) ? (array) $field['values']: null;
$colDesc['values'] = isset($field['values']) ? (array) $field['values']:null;
$validators = Doctrine::getValidators();
foreach ($validators as $validator) {
if (isset($field[$validator])) {
$colDesc[$validator] = $field[$validator];
}
}
$columns[(string) $colDesc['name']] = $colDesc;
}
}
......@@ -356,8 +372,9 @@ class Doctrine_Import_Schema
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array();
$build[$className]['options'] = isset($table['options']) ? $table['options']:array();
$build[$className]['package'] = isset($table['package']) ? $table['package']:null;
if (isset($table['inheritance'])) {
$build[$className]['inheritance'] = $table['inheritance'];
}
......
......@@ -43,7 +43,7 @@ class Doctrine_Template_Listener_Sluggable extends Doctrine_Record_Listener
'type' => 'clob',
'length' => null,
'options' => array(),
'columns' => array());
'fields' => array());
/**
* __construct
......@@ -69,12 +69,12 @@ class Doctrine_Template_Listener_Sluggable extends Doctrine_Record_Listener
protected function buildSlug($record)
{
if (empty($this->_columns)) {
if (empty($this->_options['fields'])) {
$value = (string) $record;
} else {
$value = '';
foreach ($this->_columns as $column) {
$value = $record->$column . ' ';
foreach ($this->_options['fields'] as $field) {
$value = $record->$field . ' ';
}
}
......@@ -93,4 +93,4 @@ class Doctrine_Template_Listener_Sluggable extends Doctrine_Record_Listener
return $value;
}
}
}
\ No newline at end of file
......@@ -91,9 +91,9 @@ class Doctrine_Template_Listener_Timestampable extends Doctrine_Record_Listener
$options = $this->_options[$type];
if ($options['type'] == 'date') {
return date('Y-m-d', time());
return date($options['format'], time());
} else if ($options['type'] == 'timestamp') {
return date('Y-m-d H:i:s', time());
return date($options['format'], time());
} else {
return time();
}
......
......@@ -43,7 +43,7 @@ class Doctrine_Template_Sluggable extends Doctrine_Template
'type' => 'clob',
'length' => null,
'options' => array(),
'columns' => array());
'fields' => array());
/**
* __construct
......
++ Introduction
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.
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.
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
++ Relationships
Below is an example schema file for generating a set of models.
++ Connection Binding
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.
++ Attributes
schema.yml
<code type="yml">
---
Group:
# bind this model to connection1
connection: connection1
columns:
id:
notnull: true
primary: true
autoincrement: true
type: integer
length: 4
name: id
name:
type: string
length: 255
relations:
Users:
class: User
refClass: UserGroup
# 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
</code>
And now we want to use some Doctrine code to parse that schema.yml file and generate our models from it.
++ Templates
<code type="php">
// This code will generate the models for schema.yml at /path/to/generate/models
$import = new Doctrine_Import_Schema();
$import->importSchema('schema.yml', 'yml', '/path/to/generate/models');
</code>
++ ActAs
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>
++ Options
++ Indexes
......@@ -296,123 +50,23 @@ UserProfile:
This is the PHP line of code that is auto-generated inside setTableDefinition() inside your base model class.
Note: Don't mind the extra trailing commas. This is normal and they should not affect anything negatively.
<code type="php">
<?php
class BaseUserProfile extends Doctrine_Record
{
public function setTableDefinition()
{
// code
$this->index('name_index', array('fields' => array('first_name' => array( 'sorting' => 'ASC', 'length' => '10', 'primary' => true, ), 'last_name' => array( ), ), 'type' => 'unique'));
}
}
?>
$this->index('name_index', array('fields' => array('first_name' => array( 'sorting' => 'ASC', 'length' => '10', 'primary' => true ), 'last_name' => array( ) ), 'type' => 'unique'));
</code>
++ Additional Schema Options
It isn't necessary to define both sides of a relationship in the schema.yml file as doctrine will attempt to autocomplete the relationships for you. If you choose to define only one side of the relationship, there are two yaml options you can pass to help doctrine decide how to complete the opposite end of the relationship. For example.
schema.yml
<code type="yml">
---
Table1:
tableName: table_1
relations:
Table2Alias:
class: Table2
local: foreign_key
type: one
foreignAlias: Table1Alias
foreignType: one
columns:
column_1: { type: string, length: 128 }
foreign_key: { type: integer, length: 4 }
Table2:
tableName: table_2
columns:
column_1: { type: string, length: 128 }
foreign_key: { type: integer, length: 4 }
</code>
This schema will define a 1-1 relationship between Table1 and Table2. You'll notice there are two new yaml entries, foreignAlias, and foreignType. ForeignAlias will define the as Alias portion of the opposite relationship, and similarily foreignType defines the reverse relationship of the opposite relationship. Defining foreignType is only necessary when you want a one-to-one relationship, but do not want to define both ends of the relationship manually. The above schema produces the following classes.
++ Generating Models
Once you have defined your schema files you need some code to
<code type="php">
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class Table1 extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('table_1');
$this->hasColumn('column_1', 'string', 128);
$this->hasColumn('foreign_key', 'integer', 4);
}
public function setUp()
{
$this->hasOne('Table2 as Table2Alias', array('local' => 'foreign_key',
'foreign' => 'id',
'onDelete' => 'CASCADE'));
}
}
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class Table2 extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('table_2');
$this->hasColumn('column_1', 'string', 128);
$this->hasColumn('foreign_key', 'integer', 4);
}
// 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
public function setUp()
{
$this->hasOne('Table1 as Table1Alias', array('local' => 'id',
'foreign' => 'foreign_key'));
}
}
</code>
As you can see doctrine fully completes the relationship for both classes. You can also use this shorter format for m-to-m relationships. Using the same User and Groups models defined previously, we create a simplified schema.yml. Whereas in the one-to-many and one-to-one the foreignAlias isn't a required field. If you choose to create many-to-many relationships using the short yaml syntax, the foreignAlias is required for proper generation.
<code type="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
foreignAlias: Users
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 }
Group:
columns:
id: { notnull: true, primary: true, autoincrement: true, type: integer, length: 4, name: id }
name: { type: string, length: 255 }
</code>
This schema will create identical classes as the fully defined schema.yml above.
\ No newline at end of file
// This code will generate the models for schema.yml at /path/to/generate/models
Doctrine::generateModelsFromYaml('schema.yml', '/path/to/generate/models', $options);
</code>
\ 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