/** * 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(); if ($interface = $fk->getInterface()) { $className = $this->declareClass($interface); } else { $className = $this->getClassNameFromTable($fkTable); } $varName = $this->getFKVarName($fk); $script .= "\n /**\n * Declares an association between this object and a {$className} object.\n *\n * @param {$className} \$v\n * @return \$this|" . $this->getObjectClassName(true) . " The current object (for fluent API support)\n * @throws PropelException\n */\n public function set" . $this->getFKPhpNameAffix($fk, false) . "({$className} \$v = null)\n {"; foreach ($fk->getMapping() as $map) { list($column, $rightValueOrColumn) = $map; if ($rightValueOrColumn instanceof Column) { $script .= "\n if (\$v === null) {\n \$this->set" . $column->getPhpName() . "(" . $this->getDefaultValueString($column) . ");\n } else {\n \$this->set" . $column->getPhpName() . "(\$v->get" . $rightValueOrColumn->getPhpName() . "());\n }\n"; } else { $val = var_export($rightValueOrColumn, true); $script .= "\n if (\$v === null) {\n \$this->set" . $column->getPhpName() . "(null);\n } else {\n \$this->set" . $column->getPhpName() . "({$val});\n }\n "; } } /* foreach local col */ $script .= "\n \$this->{$varName} = \$v;\n"; // Now add bi-directional relationship binding, taking into account whether this is // a one-to-one relationship. if ($fk->isLocalPrimaryKey()) { $script .= "\n // Add binding for other direction of this 1:1 relationship.\n if (\$v !== null) {\n \$v->set" . $this->getRefFKPhpNameAffix($fk, false) . "(\$this);\n }\n"; } else { $script .= "\n // Add binding for other direction of this n:n relationship.\n // If this object has already been added to the {$className} object, it will not be re-added.\n if (\$v !== null) {\n \$v->add" . $this->getRefFKPhpNameAffix($fk, false) . "(\$this);\n }\n"; } $script .= "\n\n return \$this;\n }\n"; }