From e1f9f069455e978843037c9bbaaae1a2aedc1aa8 Mon Sep 17 00:00:00 2001 From: Marc Philip Scholten Date: Thu, 10 Oct 2013 19:16:23 +0200 Subject: [PATCH] moved hydrate --- .../Generator/Builder/Om/ObjectBuilder.php | 171 +----------------- .../Om/templates/Object/_classBody.php.twig | 1 + .../Object/_foreignKey/_methods.php.twig | 4 +- .../Object/_foreignKey/_mutator.php.twig | 6 +- .../Om/templates/Object/_hydrate.php.twig | 70 +++++++ 5 files changed, 84 insertions(+), 168 deletions(-) create mode 100644 src/Propel/Generator/Builder/Om/templates/Object/_hydrate.php.twig diff --git a/src/Propel/Generator/Builder/Om/ObjectBuilder.php b/src/Propel/Generator/Builder/Om/ObjectBuilder.php index 269f860901..0353e0fea3 100644 --- a/src/Propel/Generator/Builder/Om/ObjectBuilder.php +++ b/src/Propel/Generator/Builder/Om/ObjectBuilder.php @@ -324,8 +324,6 @@ protected function addClassBody(&$script) $table = $this->getTable(); - $this->addHydrate($script); - $this->addRefFKMethods($script); $this->addCrossFKMethods($script); $this->addClear($script); @@ -659,172 +657,19 @@ public function addRemoveArrayElement(Column $col) return $script; } - /** - * Adds the hydrate() method, which sets attributes of the object based on a ResultSet. - * @param string &$script The script will be modified in this method. - */ - protected function addHydrate(&$script) + public function getInvalidTemporalString(Column $column) { - $this->addHydrateComment($script); - $this->addHydrateOpen($script); - $this->addHydrateBody($script); - $this->addHydrateClose($script); - } - - /** - * Adds the comment for the hydrate method - * @param string &$script The script will be modified in this method. - * @see addHydrate() - */ - protected function addHydrateComment(&$script) - { - $script .= " - /** - * Hydrates (populates) the object variables with values from the database resultset. - * - * An offset (0-based \"start column\") is specified so that objects can be hydrated - * with a subset of the columns in the resultset rows. This is needed, for example, - * for results of JOIN queries where the resultset row includes columns from two or - * more tables. - * - * @param array \$row The row returned by DataFetcher->fetch(). - * @param int \$startcol 0-based offset column which indicates which restultset column to start with. - * @param boolean \$rehydrate Whether this object is being re-hydrated from the database. - * @param string \$indexType The index type of \$row. Mostly DataFetcher->getIndexType(). - One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME - * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. - * - * @return int next starting column - * @throws PropelException - Any caught Exception will be rewrapped as a PropelException. - */"; - } - - /** - * Adds the function declaration for the hydrate method - * @param string &$script The script will be modified in this method. - * @see addHydrate() - */ - protected function addHydrateOpen(&$script) - { - $script .= " - public function hydrate(\$row, \$startcol = 0, \$rehydrate = false, \$indexType = TableMap::TYPE_NUM) - {"; - } - - /** - * Adds the function body for the hydrate method - * @param string &$script The script will be modified in this method. - * @see addHydrate() - */ - protected function addHydrateBody(&$script) - { - $table = $this->getTable(); - $platform = $this->getPlatform(); - - $tableMap = $this->getTableMapClassName(); - - $script .= " - try { -"; - $n = 0; - foreach ($table->getColumns() as $col) { - if (!$col->isLazyLoad()) { - $indexName = "TableMap::TYPE_NUM == \$indexType ? $n + \$startcol : $tableMap::translateFieldName('{$col->getPhpName()}', TableMap::TYPE_PHPNAME, \$indexType)"; - - $script .= " - - \$col = \$row[$indexName];"; - $clo = $col->getLowercasedName(); - if ($col->getType() === PropelTypes::CLOB_EMU && $this->getPlatform() instanceof OraclePlatform) { - // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string... - $script .= " - \$this->$clo = stream_get_contents(\$col);"; - } elseif ($col->isLobType() && !$platform->hasStreamBlobImpl()) { - $script .= " - if (null !== \$col) { - \$this->$clo = fopen('php://memory', 'r+'); - fwrite(\$this->$clo, \$col); - rewind(\$this->$clo); - } else { - \$this->$clo = null; - }"; - } elseif ($col->isTemporalType()) { - $dateTimeClass = $this->getBuildProperty('dateTimeClass'); - if (!$dateTimeClass) { - $dateTimeClass = '\DateTime'; - } - $handleMysqlDate = false; - if ($this->getPlatform() instanceof MysqlPlatform) { - if ($col->getType() === PropelTypes::TIMESTAMP) { - $handleMysqlDate = true; - $mysqlInvalidDateString = '0000-00-00 00:00:00'; - } elseif ($col->getType() === PropelTypes::DATE) { - $handleMysqlDate = true; - $mysqlInvalidDateString = '0000-00-00'; - } - // 00:00:00 is a valid time, so no need to check for that. - } - if ($handleMysqlDate) { - $script .= " - if (\$col === '$mysqlInvalidDateString') { - \$col = null; - }"; - } - $script .= " - \$this->$clo = (null !== \$col) ? PropelDateTime::newInstance(\$col, null, '$dateTimeClass') : null;"; - } elseif ($col->isPhpPrimitiveType()) { - $script .= " - \$this->$clo = (null !== \$col) ? (".$col->getPhpType().") \$col : null;"; - } elseif ($col->getType() === PropelTypes::OBJECT) { - $script .= " - \$this->$clo = \$col;"; - } elseif ($col->getType() === PropelTypes::PHP_ARRAY) { - $cloUnserialized = $clo . '_unserialized'; - $script .= " - \$this->$clo = \$col; - \$this->$cloUnserialized = null;"; - } elseif ($col->isPhpObjectType()) { - $script .= " - \$this->$clo = (null !== \$col) ? new ".$col->getPhpType()."(\$col) : null;"; - } else { - $script .= " - \$this->$clo = \$col;"; - } - $n++; - } // if col->isLazyLoad() - } /* foreach */ - - if ($this->getBuildProperty("addSaveMethod")) { - $script .= " - \$this->resetModified(); -"; - } - - $script .= " - \$this->setNew(false); - - if (\$rehydrate) { - \$this->ensureConsistency(); + if ($this->getPlatform() instanceof MysqlPlatform) { + if($column->getType() === PropelTypes::TIMESTAMP) { + return '0000-00-00 00:00:00'; + } elseif($column->getType() === PropelTypes::DATE) { + return '0000-00-00'; } + } - return \$startcol + $n; // $n = ".$this->getTableMapClass()."::NUM_HYDRATE_COLUMNS. - - } catch (Exception \$e) { - throw new PropelException(\"Error populating ".$this->getStubObjectBuilder()->getClassName()." object\", 0, \$e); - }"; + return null; } - /** - * Adds the function close for the hydrate method - * @param string &$script The script will be modified in this method. - * @see addHydrate() - */ - protected function addHydrateClose(&$script) - { - $script .= " - } -"; - } /** * Constructs variable name for fkey-related objects. * @param ForeignKey $fk diff --git a/src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig b/src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig index c05433903a..2841e37b98 100644 --- a/src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig +++ b/src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig @@ -64,6 +64,7 @@ {% include 'Object/_primaryKey/_mutators.php.twig' with {'builder': builder} %} {% include 'Object/_copy.php.twig' with {'builder': builder} %} + {% include 'Object/_hydrate.php.twig' with {'builder': builder} %} {% include 'Object/_foreignKey/_methods.php.twig' with {'builder': builder} %} diff --git a/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_methods.php.twig b/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_methods.php.twig index c2eb70a86c..7d3f49e3de 100644 --- a/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_methods.php.twig +++ b/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_methods.php.twig @@ -2,6 +2,6 @@ {% do builder.declareClassFromBuilder(builder.newStubObjectBuilder(foreignKey.foreignTable), 'Child') %} {% do builder.declareClassFromBuilder(builder.newStubQueryBuilder(foreignKey.foreignTable)) %} - {% include 'Object/_mutator.php.twig' %} - {% include 'Object/_accessor.php.twig' %} + {% include 'Object/_foreignKey/_mutator.php.twig' %} + {% include 'Object/_foreignKey/_accessor.php.twig' %} {% endfor %} \ No newline at end of file diff --git a/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_mutator.php.twig b/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_mutator.php.twig index 616339b3e4..d99403a83a 100644 --- a/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_mutator.php.twig +++ b/src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_mutator.php.twig @@ -9,11 +9,11 @@ public function set{{ builder.fKPhpNameAffix(foreignKey, false) }}({{ joinTableBuilder.objectClassName }} $v = null) { {% for columnName in foreignKey.localColumns %} - {% set column = table.column(columnName) %} + {% set column = builder.table.column(columnName) %} if ($v === null) { - $this->set{{ column.phpName }}({{ builder.defaultValueString(column.phpName) }}); + $this->set{{ column.phpName }}({{ builder.defaultValueString(column) }}); } else { - $this->set{{ column.phpName }}($v->get{{ foreignKey.table.column(foreignKey.localForeignMapping[columnName]) }}()); + $this->set{{ column.phpName }}($v->get{{ foreignKey.foreignTable.column(foreignKey.localForeignMapping[columnName]).phpName }}()); } {% endfor %} $this->{{ builder.fKVarName(foreignKey) }} = $v; diff --git a/src/Propel/Generator/Builder/Om/templates/Object/_hydrate.php.twig b/src/Propel/Generator/Builder/Om/templates/Object/_hydrate.php.twig new file mode 100644 index 0000000000..6db7f4094b --- /dev/null +++ b/src/Propel/Generator/Builder/Om/templates/Object/_hydrate.php.twig @@ -0,0 +1,70 @@ +/** + * Hydrates (populates) the object variables with values from the database resultset. + * + * An offset (0-based \"start column\") is specified so that objects can be hydrated + * with a subset of the columns in the resultset rows. This is needed, for example, + * for results of JOIN queries where the resultset row includes columns from two or + * more tables. + * + * @param array $row The row returned by DataFetcher->fetch(). + * @param int $startcol 0-based offset column which indicates which restultset column to start with. + * @param boolean $rehydrate Whether this object is being re-hydrated from the database. + * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). + One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_STUDLYPHPNAME + * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. + * + * @return int next starting column + * @throws PropelException - Any caught Exception will be rewrapped as a PropelException. + */ +public function hydrate($row, $startcol = 0, $rehydrate = false, $indexType = TableMap::TYPE_NUM) +{ + try { + {% set n = 0 %} + {% for column in builder.table.columns %} + {% set n = loop.index0 %} + {% if not column.lazyLoad %} + $col = $row[TableMap::TYPE_NUM == $indexType ? {{ loop.index0 }} + $startcol : {{ builder.tableMapClassName }}::translateFieldName('{{ column.phpName }}', TableMap::TYPE_PHPNAME, $indexType)]; + {% if column.lobType and builder.platform.hasStreamBlobImpl %} + $this->{{ column.name|lower }} = stream_get_contents($col); + {% elseif column.lobType %} + if (null !== $col) { + $this->{{ column.name|lower }} = fopen('php://memory', 'r+'); + fwrite($this->{{ column.name|lower }}, $col); + rewind($this->{{ column.name|lower }}); + } else { + $this->{{ column.name|lower }} = null; + } + {% elseif column.temporalType %} + {% if builder.invalidTemporalString(column) is not null %} + if ($col === '{{ builder.invalidTemporalString }}') { + $col = null; + } + {% endif %} + + $this->{{ column.name|lower }} = (null !== $col) ? PropelDateTime::newInstance($col, null, '{{ builder.buildProperty('dateTimeClass')|default('\DateTime') }}') : null; + {% elseif column.phpPrimitiveType %} + $this->{{ column.name|lower }} = (null !== $col) ? ({{ column.phpType }}) $col : null; + {% elseif column.phpArrayType %} + $this->{{ column.name|lower }} = $col; + $this->{{ column.name|lower }}_unserialized = null; + {% else %} + $this->{{ column.name|lower }} = $col; + {% endif %} + {% endif %} + {% endfor %} + + {% if builder.buildProperty('addSaveMethod') %} + $this->resetModified(); + {% endif %} + $this->setNew(false); + + if ($rehydrate) { + $this->ensureConsistency(); + } + + return $startcol + {{ n }}; // {{ n }} = {{ builder.tableMapClassName }}::NUM_HYDRATE_COLUMNS. + + } catch (Exception $e) { + throw new PropelException("Error populating {{ builder.objectClassName }} object", 0, $e); + } +} \ No newline at end of file