Implemented generation of indexes and their definitions from schema files.
This commit is contained in:
parent
4805dab42b
commit
a98961bd03
@ -128,7 +128,7 @@ END;
|
|||||||
* @param string $table
|
* @param string $table
|
||||||
* @param array $tableColumns
|
* @param array $tableColumns
|
||||||
*/
|
*/
|
||||||
public function buildTableDefinition(array $options, array $columns, array $relations)
|
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes)
|
||||||
{
|
{
|
||||||
$ret = array();
|
$ret = array();
|
||||||
|
|
||||||
@ -192,6 +192,60 @@ END;
|
|||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($indexes as $indexName => $definitions) {
|
||||||
|
$ret[$i] = "\n".' $this->index(\'' . $indexName . '\', array(';
|
||||||
|
|
||||||
|
foreach ($definitions as $name => $value) {
|
||||||
|
|
||||||
|
// parse fields
|
||||||
|
if ($name === 'fields') {
|
||||||
|
$ret[$i] .= '\'fields\' => array(';
|
||||||
|
|
||||||
|
foreach ($value as $fieldName => $fieldValue) {
|
||||||
|
$ret[$i] .= '\'' . $fieldName . '\' => array( ';
|
||||||
|
|
||||||
|
// parse options { sorting, length, primary }
|
||||||
|
if (isset($fieldValue) && $fieldValue) {
|
||||||
|
foreach ($fieldValue as $optionName => $optionValue) {
|
||||||
|
|
||||||
|
$ret[$i] .= '\'' . $optionName . '\' => ';
|
||||||
|
|
||||||
|
// check primary option, mark either as true or false
|
||||||
|
if ($optionName === 'primary') {
|
||||||
|
$ret[$i] .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert sorting option to uppercase, for instance, asc -> ASC
|
||||||
|
if ($optionName === 'sorting') {
|
||||||
|
$ret[$i] .= '\'' . strtoupper($optionValue) . '\', ';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the rest of the options
|
||||||
|
$ret[$i] .= '\'' . $optionValue . '\', ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret[$i] .= '), ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse index type option, 4 choices { unique, fulltext, gist, gin }
|
||||||
|
if ($name === 'type') {
|
||||||
|
$ret[$i] .= '), \'type\' => \'' . $value . '\'';
|
||||||
|
}
|
||||||
|
|
||||||
|
// add extra ) if type definition is not declared
|
||||||
|
if (!isset($definitions['type'])) {
|
||||||
|
$ret[$i] .= ')';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret[$i] .= '));';
|
||||||
|
$i++;
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($ret)) {
|
if (!empty($ret)) {
|
||||||
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
|
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
|
||||||
}
|
}
|
||||||
@ -268,7 +322,7 @@ END;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildDefinition(array $options, array $columns, array $relations = array())
|
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array())
|
||||||
{
|
{
|
||||||
if ( ! isset($options['className'])) {
|
if ( ! isset($options['className'])) {
|
||||||
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
||||||
@ -277,7 +331,7 @@ END;
|
|||||||
$abstract = isset($options['abstract']) ? 'abstract ':null;
|
$abstract = isset($options['abstract']) ? 'abstract ':null;
|
||||||
$className = $options['className'];
|
$className = $options['className'];
|
||||||
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
|
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
|
||||||
$definition = !isset($options['no_definition']) ? $this->buildTableDefinition($options, $columns, $relations):null;
|
$definition = !isset($options['no_definition']) ? $this->buildTableDefinition($options, $columns, $relations, $indexes):null;
|
||||||
$setUp = !isset($options['no_definition']) ? $this->buildSetUp($options, $columns, $relations):null;
|
$setUp = !isset($options['no_definition']) ? $this->buildSetUp($options, $columns, $relations):null;
|
||||||
|
|
||||||
$content = sprintf(self::$tpl, $abstract,
|
$content = sprintf(self::$tpl, $abstract,
|
||||||
@ -289,7 +343,7 @@ END;
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildRecord(array $options, array $columns, array $relations = array())
|
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array())
|
||||||
{
|
{
|
||||||
if ( !isset($options['className'])) {
|
if ( !isset($options['className'])) {
|
||||||
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
||||||
@ -334,15 +388,15 @@ END;
|
|||||||
$options['abstract'] = true;
|
$options['abstract'] = true;
|
||||||
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
|
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
|
||||||
|
|
||||||
$this->writeDefinition($options, $columns, $relations);
|
$this->writeDefinition($options, $columns, $relations, $indexes);
|
||||||
} else {
|
} else {
|
||||||
$this->writeDefinition($options, $columns, $relations);
|
$this->writeDefinition($options, $columns, $relations, $indexes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeDefinition(array $options, array $columns, array $relations = array())
|
public function writeDefinition(array $options, array $columns, array $relations = array(), array $indexes = array())
|
||||||
{
|
{
|
||||||
$content = $this->buildDefinition($options, $columns, $relations);
|
$content = $this->buildDefinition($options, $columns, $relations, $indexes);
|
||||||
|
|
||||||
$code = "<?php\n";
|
$code = "<?php\n";
|
||||||
|
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
class Doctrine_Import_Schema
|
class Doctrine_Import_Schema
|
||||||
{
|
{
|
||||||
public $relations = array();
|
public $relations = array();
|
||||||
|
public $indexes = array();
|
||||||
public $generateBaseClasses = false;
|
public $generateBaseClasses = false;
|
||||||
|
|
||||||
public function generateBaseClasses($bool = null)
|
public function generateBaseClasses($bool = null)
|
||||||
@ -59,7 +60,7 @@ class Doctrine_Import_Schema
|
|||||||
|
|
||||||
$this->buildRelationships($array);
|
$this->buildRelationships($array);
|
||||||
|
|
||||||
return array('schema' => $array, 'relations' => $this->relations);
|
return array('schema' => $array, 'relations' => $this->relations, 'indexes' => $this->indexes);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* importSchema
|
* importSchema
|
||||||
@ -119,6 +120,11 @@ class Doctrine_Import_Schema
|
|||||||
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
|
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIndexes($properties)
|
||||||
|
{
|
||||||
|
return isset($properties['indexes']) ? $properties['indexes']:array();;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* parseSchema
|
* parseSchema
|
||||||
*
|
*
|
||||||
@ -164,6 +170,7 @@ class Doctrine_Import_Schema
|
|||||||
$build[$className]['tableName'] = $tableName;
|
$build[$className]['tableName'] = $tableName;
|
||||||
$build[$className]['columns'] = $columns;
|
$build[$className]['columns'] = $columns;
|
||||||
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
|
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
|
||||||
|
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($table['inheritance'])) {
|
if (isset($table['inheritance'])) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user