Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved lazyLoaderBody to twig

  • Loading branch information...
commit 6ed02e59113548cb896946b4f2aeb6f8f98fbdfc 1 parent 756644c
@mpscholten authored
View
76 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -343,82 +343,6 @@ protected function addHasArrayElement(&$script, Column $column)
}
/**
- * Adds the function body for the lazy loader method.
- *
- * @param string &$script
- * @param Column $column
- * TODO: made this public because of twig usage
- */
- public function addLazyLoaderBody(Column $column)
- {
- $script = '';
- $platform = $this->getPlatform();
- $clo = $column->getLowercasedName();
-
- // pdo_sqlsrv driver requires the use of PDOStatement::bindColumn() or a hex string will be returned
- if ($column->getType() === PropelTypes::BLOB && $platform instanceof SqlsrvPlatform) {
- $script .= "
- \$c = \$this->buildPkeyCriteria();
- \$c->addSelectColumn(".$this->getColumnConstant($column).");
- try {
- \$row = array(0 => null);
- \$dataFetcher = ".$this->getQueryClassName()."::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);
- if (\$dataFetcher instanceof PDODataFetcher) {
- \$dataFetcher->bindColumn(1, \$row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
- }
- \$row = \$dataFetcher->fetch(PDO::FETCH_BOUND);
- \$dataFetcher->close();";
- } else {
- $script .= "
- \$c = \$this->buildPkeyCriteria();
- \$c->addSelectColumn(".$this->getColumnConstant($column).");
- try {
- \$dataFetcher = ".$this->getQueryClassName()."::create(null, \$c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find(\$con);
- \$row = \$dataFetcher->fetch();
- \$dataFetcher->close();";
- }
-
- $script .= "
-
- \$firstColumn = \$row ? current(\$row) : null;
-";
-
- if ($column->getType() === PropelTypes::CLOB && $platform instanceof OraclePlatform) {
- // PDO_OCI returns a stream for CLOB objects, while other PDO adapters return a string...
- $script .= "
- if (\$firstColumn) {
- \$this->$clo = stream_get_contents(\$firstColumn);
- }";
- } elseif ($column->isLobType() && !$platform->hasStreamBlobImpl()) {
- $script .= "
- if (\$firstColumn !== null) {
- \$this->$clo = fopen('php://memory', 'r+');
- fwrite(\$this->$clo, \$firstColumn);
- rewind(\$this->$clo);
- } else {
- \$this->$clo = null;
- }";
- } elseif ($column->isPhpPrimitiveType()) {
- $script .= "
- \$this->$clo = (\$firstColumn !== null) ? (".$column->getPhpType().") \$firstColumn : null;";
- } elseif ($column->isPhpObjectType()) {
- $script .= "
- \$this->$clo = (\$firstColumn !== null) ? new ".$column->getPhpType()."(\$firstColumn) : null;";
- } else {
- $script .= "
- \$this->$clo = \$firstColumn;";
- }
-
- $script .= "
- \$this->".$clo."_isLoaded = true;
- } catch (Exception \$e) {
- throw new PropelException(\"Error loading value for [$clo] column on demand.\", 0, \$e);
- }";
-
- return $script;
- }
-
- /**
* Adds a push method for an array column.
* @param string &$script The script will be modified in this method.
* @param Column $col The current column.
View
44 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_lazyLoader.php.twig
@@ -12,5 +12,47 @@
protected function load{{ column.phpName }}(ConnectionInterface $con = null)
{
- {{ builder.addLazyLoaderBody(column) }}
+ $c = $this->buildPkeyCriteria();
+ $c->addSelectColumn({{ builder.columnConstant(column) }});
+ {% if column.lobType and get_class(builder.platform) == 'Propel\Generator\Platform\SqlsrvPlatform' %}
+
+ try {
+ $row = array(0 => null);
+ $dataFetcher = {{ builder.queryClassName }}::create(null, $c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con);
+ if ($dataFetcher instanceof PDODataFetcher) {
+ $dataFetcher->bindColumn(1, $row[0], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
+ }
+ $row = $dataFetcher->fetch(PDO::FETCH_BOUND);
+ $dataFetcher->close();
+ {% else %}
+ try {
+ $dataFetcher = {{ builder.queryClassName }}::create(null, $c)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con);
+ $row = $dataFetcher->fetch();
+ $dataFetcher->close();
+ {% endif %}
+
+ $firstColumn = $row ? current($row) : null;
+ {% if column.lobType and builder.platform.hasStreamBlobImpl %}
+ if ($firstColumn) {
+ $this->{{ column.name|lower }} = stream_get_contents($firstColumn);
+ }
+ {% elseif column.lobType %}
+ if ($firstColumn !== null) {
+ $this->{{ column.name|lower }} = fopen('php://memory', 'r+');
+ fwrite($this->{{ column.name|lower }}, $firstColumn);
+ rewind($this->{{ column.name|lower }});
+ } else {
+ $this->{{ column.name|lower }} = null;
+ }
+ {% elseif column.phpPrimitiveType %}
+ $this->{{ column.name|lower }} = ($firstColumn !== null) ? ({{ column.phpType }}) $firstColumn : null;
+ {% elseif column.phpObjectType %}
+ $this->{{ column.name|lower }} = ($firstColumn !== null) ? new {{ column.phpType }}($firstColumn) : null;
+ {% else %}
+ $this->{{ column.name|lower }} = $firstColumn;
+ {% endif %}
+ $this->{{ column.name|lower }}_isLoaded = true;
+ } catch (Exception $e) {
+ throw new PropelException("Error loading value for [{{ column.name|lower }}] column on demand.", 0, $e);
+ }
}
View
4 src/Propel/Generator/Builder/PropelTwigExtension.php
@@ -83,7 +83,9 @@ public function getTests()
*/
public function getFunctions()
{
- return [];
+ return [
+ new \Twig_SimpleFunction('get_class', 'get_class'), // very hacky but required for platform checking
+ ];
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.