Skip to content

Commit

Permalink
moved hydrate
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Philip Scholten committed Oct 10, 2013
1 parent ecef289 commit e1f9f06
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 168 deletions.
171 changes: 8 additions & 163 deletions src/Propel/Generator/Builder/Om/ObjectBuilder.php
Expand Up @@ -324,8 +324,6 @@ protected function addClassBody(&$script)
$table = $this->getTable(); $table = $this->getTable();




$this->addHydrate($script);

$this->addRefFKMethods($script); $this->addRefFKMethods($script);
$this->addCrossFKMethods($script); $this->addCrossFKMethods($script);
$this->addClear($script); $this->addClear($script);
Expand Down Expand Up @@ -659,172 +657,19 @@ public function addRemoveArrayElement(Column $col)
return $script; return $script;
} }


/** public function getInvalidTemporalString(Column $column)
* 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)
{ {
$this->addHydrateComment($script); if ($this->getPlatform() instanceof MysqlPlatform) {
$this->addHydrateOpen($script); if($column->getType() === PropelTypes::TIMESTAMP) {
$this->addHydrateBody($script); return '0000-00-00 00:00:00';
$this->addHydrateClose($script); } elseif($column->getType() === PropelTypes::DATE) {
} return '0000-00-00';

/**
* 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();
} }
}


return \$startcol + $n; // $n = ".$this->getTableMapClass()."::NUM_HYDRATE_COLUMNS. return null;
} catch (Exception \$e) {
throw new PropelException(\"Error populating ".$this->getStubObjectBuilder()->getClassName()." object\", 0, \$e);
}";
} }


/**
* 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. * Constructs variable name for fkey-related objects.
* @param ForeignKey $fk * @param ForeignKey $fk
Expand Down
Expand Up @@ -64,6 +64,7 @@
{% include 'Object/_primaryKey/_mutators.php.twig' with {'builder': builder} %} {% include 'Object/_primaryKey/_mutators.php.twig' with {'builder': builder} %}


{% include 'Object/_copy.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} %} {% include 'Object/_foreignKey/_methods.php.twig' with {'builder': builder} %}


Expand Down
Expand Up @@ -2,6 +2,6 @@
{% do builder.declareClassFromBuilder(builder.newStubObjectBuilder(foreignKey.foreignTable), 'Child') %} {% do builder.declareClassFromBuilder(builder.newStubObjectBuilder(foreignKey.foreignTable), 'Child') %}
{% do builder.declareClassFromBuilder(builder.newStubQueryBuilder(foreignKey.foreignTable)) %} {% do builder.declareClassFromBuilder(builder.newStubQueryBuilder(foreignKey.foreignTable)) %}


{% include 'Object/_mutator.php.twig' %} {% include 'Object/_foreignKey/_mutator.php.twig' %}
{% include 'Object/_accessor.php.twig' %} {% include 'Object/_foreignKey/_accessor.php.twig' %}
{% endfor %} {% endfor %}
Expand Up @@ -9,11 +9,11 @@
public function set{{ builder.fKPhpNameAffix(foreignKey, false) }}({{ joinTableBuilder.objectClassName }} $v = null) public function set{{ builder.fKPhpNameAffix(foreignKey, false) }}({{ joinTableBuilder.objectClassName }} $v = null)
{ {
{% for columnName in foreignKey.localColumns %} {% for columnName in foreignKey.localColumns %}
{% set column = table.column(columnName) %} {% set column = builder.table.column(columnName) %}
if ($v === null) { if ($v === null) {
$this->set{{ column.phpName }}({{ builder.defaultValueString(column.phpName) }}); $this->set{{ column.phpName }}({{ builder.defaultValueString(column) }});
} else { } 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 %} {% endfor %}
$this->{{ builder.fKVarName(foreignKey) }} = $v; $this->{{ builder.fKVarName(foreignKey) }} = $v;
Expand Down
70 changes: 70 additions & 0 deletions 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);
}
}

0 comments on commit e1f9f06

Please sign in to comment.