/** * Adds the body of the close part of a mutator. * * @param string &$script * @param Column $column */ protected function addMutatorCloseBody(&$script, Column $column) { $table = $this->getTable(); if ($column->isForeignKey()) { foreach ($column->getForeignKeys() as $fk) { $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName()); $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($column->getName())); if (!$colFK) { continue; } $varName = $this->getFKVarName($fk); $script .= "\n if (\$this->{$varName} !== null && \$this->" . $varName . "->get" . $colFK->getPhpName() . "() !== \$v) {\n \$this->{$varName} = null;\n }\n"; } // foreach fk } /* if col is foreign key */ foreach ($column->getReferrers() as $refFK) { $tblFK = $this->getDatabase()->getTable($refFK->getForeignTableName()); if ($tblFK->getName() != $table->getName()) { foreach ($column->getForeignKeys() as $fk) { $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName()); $colFK = $tblFK->getColumn($fk->getMappedForeignColumn($column->getName())); if ($refFK->isLocalPrimaryKey()) { $varName = $this->getPKRefFKVarName($refFK); $script .= "\n // update associated " . $tblFK->getPhpName() . "\n if (\$this->{$varName} !== null) {\n \$this->{$varName}->set" . $colFK->getPhpName() . "(\$v);\n }\n"; } else { $collName = $this->getRefFKCollVarName($refFK); $script .= "\n\n // update associated " . $tblFK->getPhpName() . "\n if (\$this->{$collName} !== null) {\n foreach (\$this->{$collName} as \$referrerObject) {\n \$referrerObject->set" . $colFK->getPhpName() . "(\$v);\n }\n }\n"; } // if (isLocalPrimaryKey } // foreach col->getPrimaryKeys() } // if tablFk != table } // foreach }
public function testClearForeignKeys() { $fks = array($this->getMock('Propel\\Generator\\Model\\ForeignKey'), $this->getMock('Propel\\Generator\\Model\\ForeignKey')); $table = $this->getTableMock('books'); $table->expects($this->any())->method('getColumnForeignKeys')->with('author_id')->will($this->returnValue($fks)); $column = new Column('author_id'); $column->setTable($table); $column->addReferrer($fks[0]); $column->addReferrer($fks[1]); $this->assertTrue($column->isForeignKey()); $this->assertTrue($column->hasMultipleFK()); $this->assertTrue($column->hasReferrers()); $this->assertTrue($column->hasReferrer($fks[0])); $this->assertCount(2, $column->getReferrers()); // Clone the current column $clone = clone $column; $column->clearReferrers(); $this->assertCount(0, $column->getReferrers()); $this->assertCount(0, $clone->getReferrers()); }