Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved accessor code to templates

  • Loading branch information...
commit 9df0b7d9a866ae915ac1b2df03395133254df343 1 parent 6f7d09f
@mpscholten authored
View
4 src/Propel/Generator/Behavior/I18n/I18nBehaviorObjectBuilderModifier.php
@@ -156,7 +156,7 @@ protected function addGetCurrentTranslation()
// FIXME: the connection used by getCurrentTranslation in the generated code
// cannot be specified by the user
protected function addTranslatedColumnGetter(Column $column)
- {
+ {/*
$objectBuilder = $this->builder->getNewObjectBuilder($this->behavior->getI18nTable());
$comment = '';
$functionStatement = '';
@@ -176,7 +176,7 @@ protected function addTranslatedColumnGetter(Column $column)
'functionStatement' => $functionStatement,
'columnPhpName' => $column->getPhpName(),
'params' => implode(', ', $params[0]),
- ));
+ ));*/
}
// FIXME: the connection used by getCurrentTranslation in the generated code
View
256 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -303,7 +303,6 @@ protected function addClassBody(&$script)
$table = $this->getTable();
- $this->addColumnAccessorMethods($script);
$this->addColumnMutatorMethods($script);
$this->addHasOnlyDefaultValues($script);
@@ -481,120 +480,7 @@ public function addTemporalAccessorComment(Column $column)
return $script;
}
- /**
- * Adds an object getter method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addObjectAccessor(&$script, Column $column)
- {
- $this->addDefaultAccessorComment($script, $column);
- $this->addDefaultAccessorOpen($script, $column);
- $this->addObjectAccessorBody($script, $column);
- $this->addDefaultAccessorClose($script);
- }
-
- /**
- * Adds the function body for an object accessor method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addObjectAccessorBody(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $cloUnserialized = $clo.'_unserialized';
- if ($column->isLazyLoad()) {
- $script .= $this->getAccessorLazyLoadSnippet($column);
- }
-
- $script .= "
- if (null == \$this->$cloUnserialized && null !== \$this->$clo) {
- \$this->$cloUnserialized = unserialize(\$this->$clo);
- }
-
- return \$this->$cloUnserialized;";
- }
- /**
- * Adds an array getter method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addArrayAccessor(&$script, Column $column)
- {
- $this->addDefaultAccessorComment($script, $column);
- $this->addDefaultAccessorOpen($script, $column);
- $this->addArrayAccessorBody($script, $column);
- $this->addDefaultAccessorClose($script);
- }
-
- /**
- * Adds the function body for an array accessor method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addArrayAccessorBody(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $cloUnserialized = $clo.'_unserialized';
- if ($column->isLazyLoad()) {
- $script .= $this->getAccessorLazyLoadSnippet($column);
- }
-
- $script .= "
- if (null === \$this->$cloUnserialized) {
- \$this->$cloUnserialized = array();
- }
- if (!\$this->$cloUnserialized && null !== \$this->$clo) {
- \$$cloUnserialized = substr(\$this->$clo, 2, -2);
- \$this->$cloUnserialized = \$$cloUnserialized ? explode(' | ', \$$cloUnserialized) : array();
- }
-
- return \$this->$cloUnserialized;";
- }
-
- /**
- * Adds an enum getter method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addEnumAccessor(&$script, Column $column)
- {
- $this->addDefaultAccessorComment($script, $column);
- $this->addDefaultAccessorOpen($script, $column);
- $this->addEnumAccessorBody($script, $column);
- $this->addDefaultAccessorClose($script);
- }
-
- /**
- * Adds the function body for an enum accessor method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addEnumAccessorBody(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- if ($column->isLazyLoad()) {
- $script .= $this->getAccessorLazyLoadSnippet($column);
- }
-
- $script .= "
- if (null === \$this->$clo) {
- return null;
- }
- \$valueSet = " . $this->getTableMapClassName() . "::getValueSet(" . $this->getColumnConstant($column) . ");
- if (!isset(\$valueSet[\$this->$clo])) {
- throw new PropelException('Unknown stored enum key: ' . \$this->$clo);
- }
-
- return \$valueSet[\$this->$clo];";
- }
/**
* Adds a tester method for an array column.
@@ -638,94 +524,6 @@ protected function addHasArrayElement(&$script, Column $column)
}
/**
- * Adds a normal (non-temporal) getter method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addDefaultAccessor(&$script, Column $column)
- {
- $this->addDefaultAccessorComment($script, $column);
- $this->addDefaultAccessorOpen($script, $column);
- $this->addDefaultAccessorBody($script, $column);
- $this->addDefaultAccessorClose($script);
- }
-
- /**
- * Add the comment for a default accessor method (a getter).
- *
- * @param string &$script
- * @param Column $column
- */
- public function addDefaultAccessorComment(&$script, Column $column)
- {
- $clo=$column->getLowercasedName();
-
- $script .= "
- /**
- * Get the [$clo] column value.
- * ".$column->getDescription();
- if ($column->isLazyLoad()) {
- $script .= "
- * @param ConnectionInterface An optional ConnectionInterface connection to use for fetching this lazy-loaded column.";
- }
- $script .= "
- * @return ".$column->getPhpType()."
- */";
- }
-
- /**
- * Adds the function declaration for a default accessor.
- *
- * @param string &$script
- * @param Column $column
- */
- public function addDefaultAccessorOpen(&$script, Column $column)
- {
- $cfc = $column->getPhpName();
- $visibility = $column->getAccessorVisibility();
-
- $script .= "
- ".$visibility." function get$cfc(";
- if ($column->isLazyLoad()) {
- $script .= "ConnectionInterface \$con = null";
- }
-
- $script .= ")
- {";
- }
-
- /**
- * Adds the function body for a default accessor method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addDefaultAccessorBody(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- if ($column->isLazyLoad()) {
- $script .= $this->getAccessorLazyLoadSnippet($column);
- }
-
- $script .= "
-
- return \$this->$clo;";
- }
-
- /**
- * Adds the function close for a default accessor method.
- *
- * @param string &$script
- */
- protected function addDefaultAccessorClose(&$script)
- {
- $script .= "
- }
-";
- }
-
- /**
* Adds the lazy loader method.
*
* @param string &$script
@@ -733,58 +531,21 @@ protected function addDefaultAccessorClose(&$script)
*/
protected function addLazyLoader(&$script, Column $column)
{
- $this->addLazyLoaderComment($script, $column);
- $this->addLazyLoaderOpen($script, $column);
$this->addLazyLoaderBody($script, $column);
$this->addLazyLoaderClose($script);
}
- /**
- * Adds the comment for the lazy loader method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addLazyLoaderComment(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
-
- $script .= "
- /**
- * Load the value for the lazy-loaded [$clo] column.
- *
- * This method performs an additional query to return the value for
- * the [$clo] column, since it is not populated by
- * the hydrate() method.
- *
- * @param \$con ConnectionInterface (optional) The ConnectionInterface connection to use.
- * @return void
- * @throws PropelException - any underlying error will be wrapped and re-thrown.
- */";
- }
-
- /**
- * Adds the function declaration for the lazy loader method.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addLazyLoaderOpen(&$script, Column $column)
- {
- $cfc = $column->getPhpName();
- $script .= "
- protected function load$cfc(ConnectionInterface \$con = null)
- {";
- }
/**
* Adds the function body for the lazy loader method.
*
* @param string &$script
* @param Column $column
+ * TODO: made this public because of twig usage
*/
- protected function addLazyLoaderBody(&$script, Column $column)
+ public function addLazyLoaderBody(Column $column)
{
+ $script = '';
$platform = $this->getPlatform();
$clo = $column->getLowercasedName();
@@ -847,17 +608,8 @@ protected function addLazyLoaderBody(&$script, Column $column)
} catch (Exception \$e) {
throw new PropelException(\"Error loading value for [$clo] column on demand.\", 0, \$e);
}";
- }
- /**
- * Adds the function close for the lazy loader.
- *
- * @param string &$script
- */
- protected function addLazyLoaderClose(&$script)
- {
- $script .= "
- }";
+ return $script;
}
/**
View
0  .../Object/_accessorLazyLoadSnippet.php.twig → ...ccessor/_accessorLazyLoadSnippet.php.twig
File renamed without changes
View
13 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_arrayAccessor.php.twig
@@ -0,0 +1,13 @@
+{% embed 'Object/_acccessor/_baseAccessor.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ if (null === $this->{{ column.name|lower }}_unserialized) {
+ $this->{{ column.name|lower }}_unserialized = array();
+ }
+ if (!$this->{{ column.name|lower }} && null !== $this->{{ column.name|lower }}) {
+ ${{ column.name|lower }}_unserialized = substr($this->{{ column.name|lower }}, 2, -2);
+ $this->{{ column.name|lower }}_unserialized = ${{ column.name|lower }}_unserialized ? explode(' | ', ${{ column.name|lower }}_unserialized) : array();
+ }
+
+ return $this->{{ column.name|lower }}_unserialized;
+ {% endblock %}
+{% endembed %}
View
38 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_baseAccessor.php.twig
@@ -0,0 +1,38 @@
+{% block comment %}
+ /**
+ * Get the [{{ column.name|lower }}] column value.
+ * {{ column.description }}
+ {% if column.lazyLoad %}
+ * @param ConnectionInterface An optional ConnectionInterface connection to use for fetching this lazy-loaded column.
+ {% endif %}
+ * @return {{ column.phpType }}
+ */
+{% endblock %}
+
+
+{{ column.accessorVisibility }} function get{{ column.phpName }}(
+{% block parameters %}
+ {% if not parameters is defined %}
+ {% set parameters = [] %}
+ {% endif %}
+ {% if column.lazyLoad %}
+ {% set parameters = parameters|merge([{'type': 'ConnectionInterface', 'name': 'con', 'default': 'null'}]) %}
+ {% endif %}
+ {% for parameter in parameters %}
+ {% if parameter.type %}
+ {{ parameter.type }}
+ {% endif %}
+ ${{ parameter.name }}
+ {% if parameter.default %}
+ = {{ parameter.default }}
+ {% endif %}
+ {% endfor %}
+{% endblock %}
+)
+{
+ {% block code %}
+ {% if column.lazyLoad %}
+ {% include 'Object/_accessor/_accessorLazyLoadSnippets.php.twig' with {'builder': builder, 'column': column} %}
+ {% endif %}
+ {% endblock %}
+}
View
5 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_defaultAccessor.php.twig
@@ -0,0 +1,5 @@
+{% embed 'Object/_accessor/_baseAccessor.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ return $this->{{ column.name|lower }};
+ {% endblock %}
+{% endembed %}
View
14 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_enumAccessor.php.twig
@@ -0,0 +1,14 @@
+{% embed 'Object/_acccessor/_baseAccessor.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ if (null === $this->{{ column.name|lower }}) {
+ return null;
+ }
+
+ $valueSet = {{ builder.tableMapClassName }}::getValueSet({{ builder.columnConstant(column) }});
+ if (!isset($valueSet[$this->{{ column.name|lower }}])) {
+ throw new PropelException('Unknown stored enum key: ' . \$this->$clo);
+ }
+
+ return $valueSet[$this->{{ column.name|lower }}];"
+ {% endblock %}
+{% endembed %}
View
16 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_lazyLoader.php.twig
@@ -0,0 +1,16 @@
+/**
+ * Load the value for the lazy-loaded [{{ column.name|lower }}] column.
+ *
+ * This method performs an additional query to return the value for
+ * the [{{ column.name|lower }}] column, since it is not populated by
+ * the hydrate() method.
+ *
+ * @param $con ConnectionInterface (optional) The ConnectionInterface connection to use.
+ * @return void
+ * @throws PropelException - any underlying error will be wrapped and re-thrown.
+ */
+
+protected function load{{ column.phpName }}(ConnectionInterface $con = null)
+{
+ {{ builder.addLazyLoaderBody(column) }}
+}
View
14 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_objectAccessor.php.twig
@@ -0,0 +1,14 @@
+{% embed 'Object/_accessor/_baseAccessor.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ {{ parent() }}
+
+ $clo = $column->getLowercasedName();
+ $cloUnserialized = $clo.'_unserialized';
+
+ if (null == $this->{{ column.name|lower }}_unserialized && null !== $this->{{ column.name|lower }}) {
+ $this->{{ column.name|lower }}_unserialized = unserialize($this->{{ column.name|lower }});
+ }
+
+ return $this->{{ column.name|lower }}_unserialized;
+ {% endblock %}
+{% endembed %}
View
29 src/Propel/Generator/Builder/Om/templates/Object/_accessor/_temporalAccessor.php.twig
@@ -0,0 +1,29 @@
+{% set defaultFormat = null %}
+
+{% if column.type == constant('Propel\\Generator\\Model\\PropelTypes::DATE') %}
+ {% set defaultFormat = builder.buildProperty('defaultDateFormat') %}
+{% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIME') %}
+ {% set defaultFormat = builder.buildProperty('defaultTimeFormat') %}
+{% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIMESTAMP') %}
+ {% set defaultFormat = builder.buildProperty('defaultTimeStampFormat') %}
+{% endif %}
+
+{% embed 'Object/_accessor/_baseAccessor.php.twig' with {'builder': builder, 'column': column} %}
+ {% block comment %}
+ {{ builder.addTemporalAccessorComment(column) }} {# todo: remove this ugly hack #}
+ {% endblock %}
+ {% block parameters %}
+ {% set parameters = [{'name': 'format', 'default': defaultFormat|varExport, 'type': null}] %}
+ {{ parent() }}
+ {% endblock %}
+
+ {% block code %}
+ {{ parent() }}
+ {% do builder.declareClasses(builder.buildProperty('dateTimeClass')|default('\DateTime')) %}
+ if($format === null) {
+ return $this->{{ column.name|lower }};
+ } else {
+ return $this->{{ column.name|lower }} instanceof \DateTime ? $this->{{ column.name|lower }}->format($format) : null;
+ }
+ {% endblock %}
+{% endembed %}
View
1  src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig
@@ -28,3 +28,4 @@
{% include 'Object/_constructor.php.twig' with {'builder': builder} %}
{% include 'Object/_baseObjectMethods.php.twig' with {'builder': builder} %}
+{% include 'Object/_columnAccessorMethods.php.twig' with {'builder': builder} %}
View
43 src/Propel/Generator/Builder/Om/templates/Object/_columnAccessorMethods.php.twig
@@ -1,44 +1,17 @@
{% for column in builder.table.columns %}
- {% if column.type == constant('Propel\\Generator\\Model\\PropelTypes::DATE')
- or column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIME')
- or column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIMESTAMP')
- %}
- {{ builder.addTemporalAccessorComment(column) }} {# todo: remove this ugly hack #}
- {% set defaultFormat = null %}
-
- {% if column.type == constant('Propel\\Generator\\Model\\PropelTypes::DATE') %}
- {% set defaultFormat = builder.buildProperty('defaultDateFormat') %}
- {% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIME') %}
- {% set defaultFormat = builder.buildProperty('defaultTimeFormat') %}
- {% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::TIMESTAMP') %}
- {% set defaultFormat = builder.buildProperty('defaultTimeStampFormat') %}
- {% endif %}
-
- {{ column.accessorVisibility }} function get{{ column.phpName }}(
- $format = {{ defaultFormat|varExport }}
- {% if column.lazyLoad %}
- , $con = null)
- {% endif %}
- );
- {
- {% do builder.declareClasses(builder.buildProperty('dateTimeClass')|default('\DateTime')) %}
- {% if column.lazyLoad %}
- {% include 'Object/_accessorLazyLoadSnippets' with {'builder': builder, 'column': column} %}
- {% endif %}
-
- if($format === null) {
- return $this->{{ column.name|lower }};
- } else {
- return $this->{{ column.name|lower }} instanceof \DateTime ? $this->{{ column.name|lower }}->format($format) : null;s
- }
- }
-
+ {% if column.temporalType %}
+ {% include 'Object/_accessor/_temporalAccessor.php.twig' with {'column': column, 'builder': builder} %}
{% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::OBJECT') %}
- {% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::PHP_ARRAY') %}
+ {% include 'Object/_accessor/_objectAccessor.php.twig' with {'column': column, 'builder': builder} %}
+ {% elseif column.phpArrayType %}
+ {% include 'Object/_accessor/_arrayAccessor.php.twig' with {'column': column, 'builder': builder} %}
{% elseif column.enumType %}
+ {% include 'Object/_accessor/_enumAccessor.php.twig' with {'column': column, 'builder': builder} %}
{% else %}
+ {% include 'Object/_accessor/_defaultAccessor.php.twig' with {'column': column, 'builder': builder} %}
{% endif %}
{% if column.lazyLoad %}
+ {% include 'Object/_accessor/_lazyLoader.php.twig' with {'column': column, 'builder': builder} %}
{% endif %}
{% endfor %}
Please sign in to comment.
Something went wrong with that request. Please try again.