Permalink
Browse files

Moved mutators

  • Loading branch information...
1 parent b791e9f commit be81c46858ecf7cd6f99f28b45e41ff3bbf79b41 @mpscholten committed Oct 5, 2013
View
17 src/Propel/Generator/Behavior/I18n/templates/Object/_translatedColumnMutator.php.twig
@@ -0,0 +1,17 @@
+{% if column.temporalType %}
+ {% embed 'Object/_mutator/_temporalMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ $this->getCurrentTranslation()->set{{ column.phpName }}($v);
+
+ return $this;
+ {% endblock %}
+ {% endembed %}
+{% else %}
+ {% embed 'Object/_mutator/_defaultMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ $this->getCurrentTranslation()->set{{ column.phpName }}($v);
+
+ return $this;
+ {% endblock %}
+ {% endembed %}
+{% endif %}
View
24 src/Propel/Generator/Builder/Om/templates/Object/_columnMutatorMethods.php.twig
@@ -0,0 +1,24 @@
+{% for column in builder.table.columns %}
+ {% if column.lobType %}
+ {% include 'Object/_mutator/_lobMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% elseif column.temporalType %}
+ {% include 'Object/_mutator/_temporalMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% elseif column.phpObjectType %}
+ {% include 'Object/_mutator/_objectMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% elseif column.phpArrayType %}
+ {% include 'Object/_mutator/_arrayMutator.php.twig' with {'builder': builder, 'column': column} %}
+
+ {% if column.namePlural %}
+ {{ builder.addAddArrayElement(column) }}
+ {{ builder.addRemoveArrayElement(column) }}
+ {% endif %}
+ {% elseif column.enumType %}
+ {% include 'Object/_mutator/_enumMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% elseif column.booleanType %}
+ {% include 'Object/_mutator/_booleanMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% else %}
+ {% include 'Object/_mutator/_defaultMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% endif %}
+
+
+{% endfor %}
View
11 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_arrayMutator.php.twig
@@ -0,0 +1,11 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ {% set columnUnserialized = column.name|lower ~ '_unserialized' %}
+ if ($this->{{ columnUnserialized }} !== $v) {
+ $this->{{ columnUnserialized }} = $v;
+ $this->{{ columnUnserialized }} = '| ' . implode(' | ', $v) . ' |';
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+
+ {% endblock %}
+{% endembed %}
View
64 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_baseMutator.php.twig
@@ -0,0 +1,64 @@
+{% block comment %}
+ /**
+ * Set the value of [{{ column.name|lower }}] column.
+ * {{ column.description }}
+ * @param {{ column.phpType }} $v new value
+ * @return {{ builder.objectClassName(true) }} The current object (for fluent API support)
+ */
+{% endblock %}
+{{ column.mutatorVisibility }} function set{{ column.phpName }}($v)
+{
+{% block full_code %}
+ {% if column.lazyLoad %}
+ // explicitly set the is-loaded flag to true for this lazy load column
+ // it doesn't matter if the value is actually set or not (logic below) as
+ // any attempt to set the value means that no db lookup should be performed
+ // when the get{{ column.phpName }}() method is called.
+ $this->{{ column.name|lower }}_isLoaded = true;
+ {% endif %}
+
+ {% block code %}
+ {% endblock %}
+
+ {% if column.foreignKey %}
+ {% for fk in column.foreignKeys %}
+ {% set tableFk = builder.table.database.table(fk.foreignTableName) %}
+ {% set colFk = tableFk.column(fk.mappedForeignColumn(column.name)) %}
+ {% set varName = builder.getFKVarName(fk) %}
+
+ if($this->{{ varName }} !== null && $this->{{ varName }}->get{{ colFk.phpName }}() !== $v) {
+ $this->{{ varName }} = null;
+ }
+ {% endfor %}
+ {% endif %}
+
+ {% for refFk in column.referrers %}
+ {% set tableFk = builder.database.table(refFk.foreignTableName) %}
+
+ {% if tableFk.name != builder.table.name %}
+ {% for fk in column.foreignKeys %}
+ {% set tableFk = builder.table.database.table(fk.foreignTableName) %}
+ {% set columnFk = tableFk.column(fk.mappedForeignColumn(column.name)) %}
+
+ {% if refFk.localPrimaryKey %}
+ {% set varName = builder.getPKRefFKVarName(refPk) %}
+ // update associated {{ tableFk.phpName }}
+ if ($this->{{ varName }} !== null) {
+ $this->{{ varName }}->set{{ columnFk.phpName }}($v);
+ }
+ {% else %}
+ {% set varName = builder.refFKCollVarName(refFk) %}
+ // update associated {{ tableFk.phpName }}
+ if ($this->{{ varName }} !== null) {
+ foreach ($this->{{ varName }} as $referrerObject) {
+ $referrerObject->set{{ columnFk.phpName }}($v);
+ }
+ }
+ {% endif %}
+ {% endfor %}
+ {% endif %}
+ {% endfor %}
+
+ return $this;
+{% endblock %}
+} // set{{ column.phpName }}
View
28 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_booleanMutator.php.twig
@@ -0,0 +1,28 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block comment %}
+ /**
+ * Sets the value of the [{{ column.name|lower }}] column.
+ * Non-boolean arguments are converted using the following rules:
+ * * 1, '1', 'true', 'on', and 'yes' are converted to boolean true
+ * * 0, '0', 'false', 'off', and 'no' are converted to boolean false
+ * Check on string values is case insensitive (so 'FaLsE' is seen as 'false').
+ * {{ column.description }}
+ * @param boolean|integer|string $v The new value
+ * @return {{ builder.objectClassName(true) }} The current object (for fluent API support)
+ */
+ {% endblock %}
+ {% block code %}
+ if ($v !== null) {
+ if (is_string($v)) {
+ $v = in_array(strtolower($v), array('false', 'off', '-', 'no', 'n', '0', '')) ? false : true;
+ } else {
+ $v = (boolean) $v;
+ }
+ }
+
+ if ($this->{{ column.name|lower }} !== $v) {
+ $this->{{ column.name|lower }} = $v;
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+ {% endblock %}
+{% endembed %}
View
16 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_defaultMutator.php.twig
@@ -0,0 +1,16 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ {% if column.phpPrimitiveType %}
+ // Perform type-casting to ensure that we can use type-sensitive
+ // checking in mutators.
+ if ($v !== null) {
+ $v = ({{ column.phpType }}) $v;
+ }
+ {% endif %}
+
+ if ($this->{{ column.name|lower }} !== $v) {
+ $this->{{ column.name|lower }} = $v;
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+ {% endblock %}
+{% endembed %}
View
16 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_enumMutator.php.twig
@@ -0,0 +1,16 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ if ($v !== null) {
+ $valueSet = {{ builder.tableMapClassName }}::getValueSet({{ builder.columnConstant(column) }});
+ if (!in_array($v, $valueSet)) {
+ throw new PropelException(sprintf('Value "%s" is not accepted in this enumerated column', $v));
+ }
+ $v = array_search($v, $valueSet);
+ }
+
+ if ($this->{{ column.name|lower }} !== $v) {
+ $this->{{ column.name|lower }} = $v;
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+ {% endblock %}
+{% endembed %}
View
15 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_lobMutator.php.twig
@@ -0,0 +1,15 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ // Because BLOB columns are streams in PDO we have to assume that they are
+ // always modified when a new value is passed in. For example, the contents
+ // of the stream itself may have changed externally.
+ if (!is_resource($v) && $v !== null) {
+ $this->{{ column.name|lower }} = fopen('php://memory', 'r+');
+ fwrite($this->{{ column.name|lower }}, $v);
+ rewind($this->{{ column.name|lower }});
+ } else { // it's already a stream
+ $this->{{ column.name|lower }} = $v;
+ }
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ {% endblock %}
+{% endembed %}
View
10 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_objectMutator.php.twig
@@ -0,0 +1,10 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block code %}
+ {% set columnUnserialized = column.name|lower ~ '_unserialized' %}
+ if ($this->{{ columnUnserialized }} !== $v) {
+ $this->{{ columnUnserialized }} = $v;
+ $this->{{ columnUnserialized }} = serialize($v);
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+ {% endblock %}
+{% endembed %}
View
32 src/Propel/Generator/Builder/Om/templates/Object/_mutator/_temporalMutator.php.twig
@@ -0,0 +1,32 @@
+{% embed 'Object/_mutator/_baseMutator.php.twig' with {'builder': builder, 'column': column} %}
+ {% block comment %}
+ /**
+ * Sets the value of [{{ column.name|lower }}] column to a normalized version of the date/time value specified.
+ * {{ column.description }}
+ * @param mixed $v string, integer (timestamp), or DateTime value.
+ * Empty strings are treated as NULL.
+ * @return {{ builder.objectClassName(true) }} The current object (for fluent API support)
+ */
+ {% endblock %}
+
+ {% block code %}
+ {% set dateTimeClass = builder.buildProperty('dateTimeClass')|default('\DateTime') %}
+ {% do builder.declareClasses(dateTimeClass, '\Propel\Runtime\Util\PropelDateTime') %}
+ {% set formatter = builder.temporalFormatter(column)|varExport %}
+
+ $dt = PropelDateTime::newInstance($v, null, '{{ dateTimeClass }}');
+ if ($this->{{ column.name|lower }} !== null || $dt !== null) {
+ {% if column.defaultValue is not null and not column.defaultValue.expression %}
+ {% set defaultValue = builder.defaultValueString(column) %}
+ if ( ($dt != $this->{{ column.name|lower }}) // normalized values don't match
+ || ($dt->format($fmt) === $defaultValue) // or the entered value matches the default
+ ) {
+ {% else %}
+ if ($dt !== $this->{{ column.name|lower }}) {
+ {% endif %}
+ $this->{{ column.name|lower }} = $dt;
+ $this->modifiedColumns[] = {{ builder.columnConstant(column) }};
+ }
+ } // if either are not null
+ {% endblock %}
+{% endembed %}

0 comments on commit be81c46

Please sign in to comment.