From b4ad60384114c599d6fd846ed92c6dd44e4c3dcd Mon Sep 17 00:00:00 2001 From: zYne Date: Thu, 28 Jun 2007 11:56:56 +0000 Subject: [PATCH] --- lib/Doctrine/Import/Builder.php | 18 ---- lib/Doctrine/Record.php | 3 +- lib/Doctrine/Relation/Parser.php | 16 +++- lib/Doctrine/Table.php | 143 +++++++++++-------------------- lib/Doctrine/Template.php | 4 +- lib/Doctrine/Validator.php | 62 ++++---------- tests/TemplateTestCase.php | 6 +- tests/run.php | 9 +- 8 files changed, 96 insertions(+), 165 deletions(-) diff --git a/lib/Doctrine/Import/Builder.php b/lib/Doctrine/Import/Builder.php index 0c8ed0448..7bddac61d 100644 --- a/lib/Doctrine/Import/Builder.php +++ b/lib/Doctrine/Import/Builder.php @@ -78,7 +78,6 @@ class Doctrine_Import_Builder * This is a template that was previously in Builder/Record.tpl. Due to the fact * that it was not bundled when compiling, it had to be moved here. * - * @access public * @return void */ public function loadTemplate() @@ -114,7 +113,6 @@ END; * * @param string $table * @param array $tableColumns - * @access public */ public function buildDefinition($table, $tableColumns) { @@ -201,20 +199,4 @@ END; throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $fileName); } } - - /** - * - * @param Doctrine_Schema_Object $schema - * @throws Doctrine_Import_Exception - * @return void - */ - public function build(Doctrine_Schema_Object $schema) - { - foreach ($schema->getDatabases() as $database){ - foreach ($database->getTables() as $table){ - $this->buildRecord($table); - } - } - } - } diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 90225cbea..182eae367 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1385,7 +1385,8 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count */ public function loadTemplate($template) { - $tpl = new $template($this->_table); + $tpl = new $template(); + $tpl->setTable($this->_table); $tpl->setUp(); $tpl->setTableDefinition(); return $this; diff --git a/lib/Doctrine/Relation/Parser.php b/lib/Doctrine/Relation/Parser.php index d3854b1ab..b79275a83 100644 --- a/lib/Doctrine/Relation/Parser.php +++ b/lib/Doctrine/Relation/Parser.php @@ -101,6 +101,20 @@ class Doctrine_Relation_Parser throw new Doctrine_Relation_Exception('Relation type not set.'); } + if (strpos($name, '[Component]') !== false) { + $name = str_replace('[Component]', $this->_table->getComponentName(), $name); + $templateName = substr($name, strlen($this->_table->getComponentName())); + + if (substr($name, -8) === 'Template') { + $name = substr($name, 0, -8); + } + + if ( ! class_exists($name)) { + $template = new $templateName(); + print_r($template->getTable()->getColumns()); + } + } + $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); $m = Doctrine_Manager::getInstance(); @@ -332,7 +346,7 @@ class Doctrine_Relation_Parser if ($def['local'] !== $this->_table->getIdentifier()) { $def['localKey'] = true; } - } + } } else { if (isset($def['foreign'])) { // local key not set, but foreign key is set diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index cca9f1df4..95aaa50bf 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -231,13 +231,10 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable switch (count($this->primaryKeys)) { case 0: $this->columns = array_merge(array('id' => - array('integer', - 20, - array('autoincrement' => true, - 'primary' => true, - ) - ) - ), $this->columns); + array('type' => 'integer', + 'length' => 20, + 'autoincrement' => true, + 'primary' => true)), $this->columns); $this->primaryKeys[] = 'id'; $this->identifier = 'id'; @@ -251,7 +248,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable } else { foreach ($this->primaryKeys as $pk) { - $e = $this->columns[$pk][2]; + $e = $this->columns[$pk]; $found = false; @@ -262,26 +259,26 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $e2 = explode(':', $option); switch (strtolower($e2[0])) { - case 'autoincrement': - case 'autoinc': - $this->identifierType = Doctrine::IDENTIFIER_AUTOINC; - $found = true; - break; - case 'seq': - case 'sequence': - $this->identifierType = Doctrine::IDENTIFIER_SEQUENCE; - $found = true; - - if ($value) { - $this->options['sequenceName'] = $value; - } else { - if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) { - $this->options['sequenceName'] = $sequence; + case 'autoincrement': + case 'autoinc': + $this->identifierType = Doctrine::IDENTIFIER_AUTOINC; + $found = true; + break; + case 'seq': + case 'sequence': + $this->identifierType = Doctrine::IDENTIFIER_SEQUENCE; + $found = true; + + if ($value) { + $this->options['sequenceName'] = $value; } else { - $this->options['sequenceName'] = $this->conn->getSequenceName($this->options['tableName']); + if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) { + $this->options['sequenceName'] = $sequence; + } else { + $this->options['sequenceName'] = $this->conn->getSequenceName($this->options['tableName']); + } } - } - break; + break; } } if ( ! isset($this->identifierType)) { @@ -329,21 +326,19 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $primary = array(); foreach ($this->getColumns() as $name => $column) { - $definition = $column[2]; - $definition['type'] = $column[0]; - $definition['length'] = $column[1]; + $definition = $column; switch ($definition['type']) { - case 'enum': - if (isset($definition['default'])) { - $definition['default'] = $this->enumIndex($name, $definition['default']); - } - break; - case 'boolean': - if (isset($definition['default'])) { - $definition['default'] = $this->getConnection()->convertBooleans($definition['default']); - } - break; + case 'enum': + if (isset($definition['default'])) { + $definition['default'] = $this->enumIndex($name, $definition['default']); + } + break; + case 'boolean': + if (isset($definition['default'])) { + $definition['default'] = $this->getConnection()->convertBooleans($definition['default']); + } + break; } $columns[$name] = $definition; @@ -689,7 +684,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable throw new Doctrine_Table_Exception('Invalid argument given for column length'); } - $this->columns[$name] = array($type, $length, $options); + $this->columns[$name] = $options; + $this->columns[$name]['type'] = $type; + $this->columns[$name]['length'] = $length; if (isset($options['primary'])) { $this->primaryKeys[] = $name; @@ -721,8 +718,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if ( ! isset($this->columns[$column])) { throw new Doctrine_Table_Exception("Couldn't get default value. Column ".$column." doesn't exist."); } - if (isset($this->columns[$column][2]['default'])) { - return $this->columns[$column][2]['default']; + if (isset($this->columns[$column]['default'])) { + return $this->columns[$column]['default']; } else { return null; } @@ -871,7 +868,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable */ public function findBySql($dql, array $params = array()) { $q = new Doctrine_Query($this->conn); - $users = $q->query("FROM ".$this->options['name']." WHERE ".$dql, $params); + $users = $q->query('FROM ' . $this->options['name'] . ' WHERE ' . $dql, $params); return $users; } @@ -1010,7 +1007,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable */ public function count() { - $a = $this->conn->getDBH()->query("SELECT COUNT(1) FROM ".$this->options['tableName'])->fetch(PDO::FETCH_NUM); + $a = $this->conn->execute('SELECT COUNT(1) FROM ' . $this->options['tableName'])->fetch(Doctrine::FETCH_NUM); return current($a); } /** @@ -1022,40 +1019,14 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $graph->load($this->getComponentName()); return $graph; } - /** - * execute - * @param string $query - * @param array $array - * @param integer $limit - * @param integer $offset - */ - public function execute($query, array $array = array(), $limit = null, $offset = null) { - $coll = new Doctrine_Collection($this); - $query = $this->conn->modifyLimitQuery($query,$limit,$offset); - if ( ! empty($array)) { - $stmt = $this->conn->getDBH()->prepare($query); - $stmt->execute($array); - } else { - $stmt = $this->conn->getDBH()->query($query); - } - $data = $stmt->fetchAll(PDO::FETCH_ASSOC); - $stmt->closeCursor(); - - foreach ($data as $row) { - $this->data = $row; - $record = $this->getRecord(); - $coll->add($record); - } - return $coll; - } /** * @param string $field * @return array */ - final public function getEnumValues($field) + public function getEnumValues($field) { - if (isset($this->columns[$field][2]['values'])) { - return $this->columns[$field][2]['values']; + if (isset($this->columns[$field]['values'])) { + return $this->columns[$field]['values']; } else { return array(); } @@ -1072,7 +1043,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if ($index instanceof Doctrine_Null) return $index; - return isset($this->columns[$field][2]['values'][$index]) ? $this->columns[$field][2]['values'][$index] : $index; + return isset($this->columns[$field]['values'][$index]) ? $this->columns[$field]['values'][$index] : $index; } /** * enumIndex @@ -1087,24 +1058,12 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable return array_search($value, $values); } - /** - * getDefinitionOf - * - * @return string ValueWrapper class name on success, false on failure - */ - public function getValueWrapperOf($column) - { - if (isset($this->columns[$column][2]['wrapper'])) { - return $this->columns[$column][2]['wrapper']; - } - return false; - } /** * getColumnCount * * @return integer the number of columns in this table */ - final public function getColumnCount() + public function getColumnCount() { return $this->columnCount; } @@ -1114,7 +1073,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * * @return array */ - final public function getColumns() + public function getColumns() { return $this->columns; } @@ -1147,7 +1106,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable public function getTypeOf($column) { if (isset($this->columns[$column])) { - return $this->columns[$column][0]; + return $this->columns[$column]['type']; } return false; } @@ -1173,14 +1132,14 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable $sql = "SELECT MAX(".$this->getIdentifier().") FROM ".$this->getTableName(); $stmt = $this->conn->getDBH()->query($sql); $data = $stmt->fetch(PDO::FETCH_NUM); - return isset($data[0])?$data[0]:1; + return isset($data[0])? $data[0] : 1; } /** * returns simple cached query * * @return string */ - final public function getQuery() + public function getQuery() { return $this->query; } @@ -1190,7 +1149,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable * * @return array */ - final public function getData() + public function getData() { return $this->data; } diff --git a/lib/Doctrine/Template.php b/lib/Doctrine/Template.php index 54fc1d67c..1dee3ae1f 100644 --- a/lib/Doctrine/Template.php +++ b/lib/Doctrine/Template.php @@ -37,11 +37,11 @@ class Doctrine_Template extends Doctrine_Record_Abstract */ protected $_table; /** - * constructor, creates tree with reference to table and any options + * setTable * * @param Doctrine_Table $_table the table object this Template belongs to */ - public function __construct(Doctrine_Table $table) + public function setTable(Doctrine_Table $table) { $this->_table = $table; } diff --git a/lib/Doctrine/Validator.php b/lib/Doctrine/Validator.php index 080dd1cf3..e287d0fb3 100644 --- a/lib/Doctrine/Validator.php +++ b/lib/Doctrine/Validator.php @@ -30,27 +30,12 @@ * @version $Revision$ * @author Konsta Vesterinen */ -class Doctrine_Validator +class Doctrine_Validator extends Doctrine_Object { /** * @var array $validators an array of validator objects */ private static $validators = array(); - /** - * @var Doctrine_Null $null a Doctrine_Null object used for extremely fast - * null value testing - */ - private static $null; - /** - * initNullObject - * - * @param Doctrine_Null $null - * @return void - */ - public static function initNullObject(Doctrine_Null $null) - { - self::$null = $null; - } /** * returns a validator object * @@ -90,14 +75,15 @@ class Doctrine_Validator $err = array(); foreach ($data as $key => $value) { - if ($value === self::$null) + if ($value === self::$_null) { $value = null; - elseif ($value instanceof Doctrine_Record) + } elseif ($value instanceof Doctrine_Record) { $value = $value->getIncremented(); + } $column = $columns[$key]; - if ($column[0] == "enum") { + if ($column['type'] == 'enum') { $value = $record->getTable()->enumIndex($key, $value); if ($value === false) { @@ -114,24 +100,7 @@ class Doctrine_Validator } } - if ( ! is_array($column[2])) { - $e = explode("|",$column[2]); - } else { - $e = $column[2]; - } - - foreach ($e as $k => $arg) { - if (is_string($k)) { - $name = $k; - $args = $arg; - } else { - $args = explode(":",$arg); - $name = array_shift($args); - if ( ! isset($args[0])) { - $args[0] = ''; - } - } - + foreach ($column as $name => $args) { if (empty($name) || $name == 'primary' || $name == 'protected' @@ -139,8 +108,7 @@ class Doctrine_Validator || $name == 'default' || $name == 'values' || $name == 'sequence' - || $name == 'zerofill' - ) { + || $name == 'zerofill') { continue; } if (strtolower($name) == 'length') { @@ -154,7 +122,7 @@ class Doctrine_Validator if (strtolower($name) == 'type') { if (!$record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { - if ( ! self::isValidType($value, $column[0])) { + if ( ! self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); } } @@ -173,7 +141,7 @@ class Doctrine_Validator } if ($record->getTable()->getAttribute(Doctrine::ATTR_AUTO_TYPE_VLD)) { - if ( ! self::isValidType($value, $column[0])) { + if ( ! self::isValidType($value, $column['type'])) { $errorStack->add($key, 'type'); continue; } @@ -185,15 +153,15 @@ class Doctrine_Validator */ private function validateLength($column, $key, $value) { - if ($column[0] == "timestamp" || $column[0] == "integer") { + if ($column['type'] == 'timestamp' || $column['type'] == 'integer') { return true; - } else if ($column[0] == "array" || $column[0] == "object") { + } elseif ($column['type'] == 'array' || $column['type'] == 'object') { $length = strlen(serialize($value)); } else { $length = strlen($value); } - if ($length > $column[1]) { + if ($length > $column['length']) { return false; } return true; @@ -240,8 +208,9 @@ class Doctrine_Validator */ public static function isValidType($var, $type) { - if ($type == 'boolean') + if ($type == 'boolean') { return true; + } $looseType = self::gettype($var); $type = self::phpType($type); @@ -250,8 +219,9 @@ class Doctrine_Validator case 'float': case 'double': case 'integer': - if ($type == 'string' || $type == 'float') + if ($type == 'string' || $type == 'float') { return true; + } case 'string': case 'array': case 'object': diff --git a/tests/TemplateTestCase.php b/tests/TemplateTestCase.php index 591fceda3..b82db03cb 100644 --- a/tests/TemplateTestCase.php +++ b/tests/TemplateTestCase.php @@ -32,8 +32,12 @@ */ class Doctrine_Template_TestCase extends Doctrine_UnitTestCase { + public function testTemplateRelationsSupportConcreteInheritance() { - $blog = new Blog(); + $blog = new Blog(); + + $this->conn->export->exportClassesSql(array('Blog')); } + } diff --git a/tests/run.php b/tests/run.php index 749e1ad90..672a0c6b0 100644 --- a/tests/run.php +++ b/tests/run.php @@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); $test->addTestCase(new Doctrine_Ticket330_TestCase()); */ - /** */ +/** */ // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); @@ -302,14 +302,15 @@ $test->addTestCase(new Doctrine_Query_TestCase()); $test->addTestCase(new Doctrine_Ticket364_TestCase()); -$test->addTestCase(new Doctrine_Template_TestCase()); - $test->addTestCase(new Doctrine_Query_MysqlSubquery_TestCase()); $test->addTestCase(new Doctrine_Query_PgsqlSubquery_TestCase()); $test->addTestCase(new Doctrine_Query_MysqlSubqueryHaving_TestCase()); - + /***/ + /** +$test->addTestCase(new Doctrine_Template_TestCase()); + */ //$test->addTestCase(new Doctrine_IntegrityAction_TestCase()); //$test->addTestCase(new Doctrine_AuditLog_TestCase());