Permalink
Browse files

moved foreign key mutator

  • Loading branch information...
1 parent 0af0fc8 commit ecef2897530b77a17397d6deefbd4a50027b5479 @mpscholten committed Oct 10, 2013
View
91 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -326,7 +326,6 @@ protected function addClassBody(&$script)
$this->addHydrate($script);
- $this->addFKMethods($script);
$this->addRefFKMethods($script);
$this->addCrossFKMethods($script);
$this->addClear($script);
@@ -858,20 +857,6 @@ public function getPKRefFKVarName(ForeignKey $fk)
}
/**
- * Adds the methods that get & set objects related by foreign key to the current object.
- * @param string &$script The script will be modified in this method.
- */
- protected function addFKMethods(&$script)
- {
- foreach ($this->getTable()->getForeignKeys() as $fk) {
- $this->declareClassFromBuilder($this->getNewStubObjectBuilder($fk->getForeignTable()), 'Child');
- $this->declareClassFromBuilder($this->getNewStubQueryBuilder($fk->getForeignTable()));
- $this->addFKMutator($script, $fk);
- $this->addFKAccessor($script, $fk);
- } // foreach fk
- }
-
- /**
* Adds the class attributes that are needed to store fkey related objects.
* @param string &$script The script will be modified in this method.
* @param ForeignKey $fk
@@ -890,84 +875,13 @@ protected function addFKAttributes(&$script, ForeignKey $fk)
}
/**
- * Adds the mutator (setter) method for setting an fkey related object.
- * @param string &$script The script will be modified in this method.
- * @param ForeignKey $fk
- */
- protected function addFKMutator(&$script, ForeignKey $fk)
- {
- $table = $this->getTable();
- $fkTable = $fk->getForeignTable();
-
- $joinTableObjectBuilder = $this->getNewObjectBuilder($fkTable);
- $className = $joinTableObjectBuilder->getObjectClassName();
-
- $varName = $this->getFKVarName($fk);
-
- $script .= "
- /**
- * Declares an association between this object and a $className object.
- *
- * @param $className \$v
- * @return ".$this->getObjectClassName(true)." The current object (for fluent API support)
- * @throws PropelException
- */
- public function set".$this->getFKPhpNameAffix($fk, false)."($className \$v = null)
- {";
- foreach ($fk->getLocalColumns() as $columnName) {
- $column = $table->getColumn($columnName);
- $lfmap = $fk->getLocalForeignMapping();
- $colFKName = $lfmap[$columnName];
- $colFK = $fkTable->getColumn($colFKName);
- $script .= "
- if (\$v === null) {
- \$this->set".$column->getPhpName()."(".$this->getDefaultValueString($column).");
- } else {
- \$this->set".$column->getPhpName()."(\$v->get".$colFK->getPhpName()."());
- }
-";
-
- } /* foreach local col */
-
- $script .= "
- \$this->$varName = \$v;
-";
-
- // Now add bi-directional relationship binding, taking into account whether this is
- // a one-to-one relationship.
-
- if ($fk->isLocalPrimaryKey()) {
- $script .= "
- // Add binding for other direction of this 1:1 relationship.
- if (\$v !== null) {
- \$v->set".$this->getRefFKPhpNameAffix($fk, false)."(\$this);
- }
-";
- } else {
- $script .= "
- // Add binding for other direction of this n:n relationship.
- // If this object has already been added to the $className object, it will not be re-added.
- if (\$v !== null) {
- \$v->add".$this->getRefFKPhpNameAffix($fk, false)."(\$this);
- }
-";
-
- }
-
- $script .= "
-
- return \$this;
- }
-";
- }
-
- /**
* Adds the accessor (getter) method for getting an fkey related object.
* @param string &$script The script will be modified in this method.
* @param ForeignKey $fk
*/
- protected function addFKAccessor(&$script, ForeignKey $fk)
+ public function addFKAccessor(ForeignKey $fk)
{
+ $script = '';
$table = $this->getTable();
$varName = $this->getFKVarName($fk);
@@ -1055,6 +969,7 @@ public function get".$this->getFKPhpNameAffix($fk, false)."(ConnectionInterface
}
";
+ return $script;
} // addFKAccessor
/**
View
2 src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig
@@ -65,6 +65,8 @@
{% include 'Object/_copy.php.twig' with {'builder': builder} %}
+ {% include 'Object/_foreignKey/_methods.php.twig' with {'builder': builder} %}
+
{% if builder.generatorConfig.buildProperty('addHooks') %}
{% include 'Object/_hookMethods.php.twig' with {'builder': builder} %}
{% endif %}
View
1 src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_accessor.php.twig
@@ -0,0 +1 @@
+{{ builder.addFKAccessor(foreignKey) }}
View
7 src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_methods.php.twig
@@ -0,0 +1,7 @@
+{% for foreignKey in builder.table.foreignKeys %}
+ {% do builder.declareClassFromBuilder(builder.newStubObjectBuilder(foreignKey.foreignTable), 'Child') %}
+ {% do builder.declareClassFromBuilder(builder.newStubQueryBuilder(foreignKey.foreignTable)) %}
+
+ {% include 'Object/_mutator.php.twig' %}
+ {% include 'Object/_accessor.php.twig' %}
+{% endfor %}
View
35 src/Propel/Generator/Builder/Om/templates/Object/_foreignKey/_mutator.php.twig
@@ -0,0 +1,35 @@
+{% set joinTableBuilder = builder.newObjectBuilder(foreignKey.foreignTable) %}
+/**
+* Declares an association between this object and a $className object.
+*
+* @param {{ joinTableBuilder.objectClassName(true) }} \$v
+* @return {{ builder.objectClassName(true) }} The current object (for fluent API support)
+* @throws PropelException
+*/
+public function set{{ builder.fKPhpNameAffix(foreignKey, false) }}({{ joinTableBuilder.objectClassName }} $v = null)
+{
+ {% for columnName in foreignKey.localColumns %}
+ {% set column = table.column(columnName) %}
+ if ($v === null) {
+ $this->set{{ column.phpName }}({{ builder.defaultValueString(column.phpName) }});
+ } else {
+ $this->set{{ column.phpName }}($v->get{{ foreignKey.table.column(foreignKey.localForeignMapping[columnName]) }}());
+ }
+ {% endfor %}
+ $this->{{ builder.fKVarName(foreignKey) }} = $v;
+
+ {% if foreignKey.localPrimaryKey %}
+ // Add binding for other direction of this 1:1 relationship.
+ if ($v !== null) {
+ $v->set{{ builder.refFKPhpNameAffix(foreignKey, false) }}($this);
+ }
+ {% else %}
+ // Add binding for other direction of this n:n relationship.
+ // If this object has already been added to the $className object, it will not be re-added.
+ if ($v !== null) {
+ $v->add{{ builder.refFKPhpNameAffix(foreignKey, false) }}($this);
+ }
+ {% endif %}
+
+ return $this;
+}

0 comments on commit ecef289

Please sign in to comment.