Permalink
Browse files

Moved some parts of the accessor methods

  • Loading branch information...
1 parent d181c0c commit e27922f11a2ae7aed34bdc4f7c60928fb52dc20a @mpscholten committed Oct 4, 2013
@@ -41,6 +41,12 @@ public function __construct(Table $table)
$this->twig = new \Twig_Environment($loader, ['autoescape' => false, 'strict_variables' => true, 'cache' => sys_get_temp_dir() . 'propel2-cache', 'auto_reload' => true]);
$this->twig->addFilter('addSlashes', new \Twig_SimpleFilter('addSlashes', 'addslashes'));
$this->twig->addFilter('lcfirst', new \Twig_SimpleFilter('lcfirst', 'lcfirst'));
+ $this->twig->addFilter(
+ 'varExport',
+ new \Twig_SimpleFilter('varExport', function ($input) {
+ return var_export($input, true);
+ })
+ );
}
@@ -438,8 +444,9 @@ protected function addTemporalAccessor(&$script, Column $column)
* @param string &$script
* @param Column $column
*/
- public function addTemporalAccessorComment(&$script, Column $column)
+ public function addTemporalAccessorComment(Column $column)
{
+ $script = '';
$clo = $column->getLowercasedName();
$dateTimeClass = $this->getBuildProperty('dateTimeClass');
@@ -471,123 +478,9 @@ public function addTemporalAccessorComment(&$script, Column $column)
*
* @throws PropelException - if unable to parse/validate the date/time value.
*/";
- }
-
- /**
- * Adds the function declaration for a temporal accessor.
- *
- * @param string &$script
- * @param Column $column
- */
- public function addTemporalAccessorOpen(&$script, Column $column)
- {
- $cfc = $column->getPhpName();
-
- $defaultfmt = null;
- $visibility = $column->getAccessorVisibility();
-
- // Default date/time formatter strings are specified in build.properties
- if ($column->getType() === PropelTypes::DATE) {
- $defaultfmt = $this->getBuildProperty('defaultDateFormat');
- } elseif ($column->getType() === PropelTypes::TIME) {
- $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
- } elseif ($column->getType() === PropelTypes::TIMESTAMP) {
- $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
- }
-
- if (empty($defaultfmt)) {
- $defaultfmt = null;
- }
-
- $script .= "
- ".$visibility." function get$cfc(\$format = ".var_export($defaultfmt, true)."";
- if ($column->isLazyLoad()) {
- $script .= ", \$con = null";
- }
- $script .= ")
- {";
- }
-
- /**
- * Gets accessor lazy loaded snippets.
- *
- * @param Column $column
- * @return string
- */
- protected function getAccessorLazyLoadSnippet(Column $column)
- {
- if ($column->isLazyLoad()) {
- $clo = $column->getLowercasedName();
- $defaultValueString = 'null';
- $def = $column->getDefaultValue();
- if ($def !== null && !$def->isExpression()) {
- $defaultValueString = $this->getDefaultValueString($column);
- }
-
- return "
- if (!\$this->{$clo}_isLoaded && \$this->{$clo} === {$defaultValueString} && !\$this->isNew()) {
- \$this->load{$column->getPhpName()}(\$con);
- }
-";
- }
- return '';
- }
-
- /**
- * Adds the body of the temporal accessor.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addTemporalAccessorBody(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
-
- $dateTimeClass = $this->getBuildProperty('dateTimeClass');
- if (!$dateTimeClass) {
- $dateTimeClass = '\DateTime';
- }
- $this->declareClasses($dateTimeClass);
- $defaultfmt = null;
-
- // Default date/time formatter strings are specified in build.properties
- if ($column->getType() === PropelTypes::DATE) {
- $defaultfmt = $this->getBuildProperty('defaultDateFormat');
- } elseif ($column->getType() === PropelTypes::TIME) {
- $defaultfmt = $this->getBuildProperty('defaultTimeFormat');
- } elseif ($column->getType() === PropelTypes::TIMESTAMP) {
- $defaultfmt = $this->getBuildProperty('defaultTimeStampFormat');
- }
-
- if (empty($defaultfmt)) {
- $defaultfmt = null;
- }
-
- if ($column->isLazyLoad()) {
- $script .= $this->getAccessorLazyLoadSnippet($column);
- }
-
- $script .= "
- if (\$format === null) {
- return \$this->$clo;
- } else {
- return \$this->$clo instanceof \DateTime ? \$this->{$clo}->format(\$format) : null;
- }";
- }
-
- /**
- * Adds the body of the temporal accessor.
- *
- * @param string &$script
- */
- protected function addTemporalAccessorClose(&$script)
- {
- $script .= "
- }
-";
+ return $script;
}
-
/**
* Adds an object getter method.
*
@@ -0,0 +1,9 @@
+{% set defaultValueString = 'null' %}
+{% set defaultValue = column.defaultValue %}
+
+{% if defaultValue != null and not defaultValue.expression %}
+ {% set defaultValueString = builder.defaultValueString(column) %}
+{% endif %}
+if (!$this->{{ column.name|lower }}_isLoaded && $this->{{ column.name|lower }} === {{ defaultValueString }} && !$this->isNew()) {
+ $this->load{{ column.phpName }}($con);
+}
@@ -0,0 +1,44 @@
+{% 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
+ }
+ }
+
+ {% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::OBJECT') %}
+ {% elseif column.type == constant('Propel\\Generator\\Model\\PropelTypes::PHP_ARRAY') %}
+ {% elseif column.enumType %}
+ {% else %}
+ {% endif %}
+
+ {% if column.lazyLoad %}
+ {% endif %}
+{% endfor %}

0 comments on commit e27922f

Please sign in to comment.