Navigation Menu

Skip to content

Commit

Permalink
fix bug when adding previously removed relations
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Jul 12, 2013
1 parent 5886ff7 commit a5226c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
21 changes: 18 additions & 3 deletions generator/lib/builder/om/PHP5ObjectBuilder.php
Expand Up @@ -3866,6 +3866,8 @@ protected function addRefFKAdd(&$script, ForeignKey $refFK)

$collName = $this->getRefFKCollVarName($refFK);

$scheduledForDeletion = lcfirst($this->getRefFKPhpNameAffix($refFK, $plural = true)) . "ScheduledForDeletion";

$script .= "
/**
* Method called to associate a $className object to this object
Expand All @@ -3880,8 +3882,13 @@ public function add" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "(
\$this->init" . $this->getRefFKPhpNameAffix($refFK, $plural = true) . "();
\$this->{$collName}Partial = true;
}
if (!in_array(\$l, \$this->{$collName}->getArrayCopy(), true)) { // only add it if the **same** object is not already associated
\$this->doAdd" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "(\$l);
if (\$this->{$scheduledForDeletion} and \$this->{$scheduledForDeletion}->contains(\$l)) {
\$this->{$scheduledForDeletion}->remove(\$this->{$scheduledForDeletion}->search(\$l));
}
}
return \$this;
Expand Down Expand Up @@ -4571,6 +4578,8 @@ protected function addCrossFKAdd(&$script, ForeignKey $refFK, ForeignKey $crossF

$relatedObjectClassName = $this->getFKPhpNameAffix($crossFK, $plural = false);

$scheduledForDeletion = lcfirst($relCol) . "ScheduledForDeletion";

$script .= "
/**
* Associate a " . $crossObjectClassName . " object to this object
Expand All @@ -4584,9 +4593,14 @@ public function add{$relatedObjectClassName}($crossObjectClassName $crossObjectN
if (\$this->" . $collName . " === null) {
\$this->init" . $relCol . "();
}
if (!\$this->" . $collName . "->contains(" . $crossObjectName . ")) { // only add it if the **same** object is not already associated
\$this->doAdd{$relatedObjectClassName}($crossObjectName);
\$this->" . $collName . "[] = " . $crossObjectName . ";
if (\$this->" . $scheduledForDeletion . " and \$this->" . $scheduledForDeletion . "->contains(" . $crossObjectName . ")) {
\$this->" . $scheduledForDeletion . "->remove(\$this->" . $scheduledForDeletion . "->search(" . $crossObjectName . "));
}
}
return \$this;
Expand Down Expand Up @@ -4620,12 +4634,13 @@ protected function addCrossFKDoAdd(&$script, ForeignKey $refFK, ForeignKey $cros
*/
protected function doAdd{$relatedObjectClassName}(\${$lowerRelatedObjectClassName})
{
{$foreignObjectName} = new {$className}();
{$foreignObjectName}->set{$relatedObjectClassName}(\${$lowerRelatedObjectClassName});
\$this->add{$refKObjectClassName}({$foreignObjectName});
// set the back reference to this object directly as using provided method either results
// in endless loop or in multiple relations
if (!\${$lowerRelatedObjectClassName}->get{$selfRelationNamePlural}()->contains(\$this)) {
{$foreignObjectName} = new {$className}();
{$foreignObjectName}->set{$relatedObjectClassName}(\${$lowerRelatedObjectClassName});
\$this->add{$refKObjectClassName}({$foreignObjectName});
\$foreignCollection = \${$lowerRelatedObjectClassName}->get{$selfRelationNamePlural}();
\$foreignCollection[] = \$this;
}
Expand Down
32 changes: 31 additions & 1 deletion test/testsuite/generator/builder/om/GeneratedObjectRelTest.php
Expand Up @@ -134,7 +134,6 @@ public function testManyToMany_Dir2_Saved()
$this->assertEquals(1, count($list->getBookListRels()) );
$this->assertEquals(1, count($book->getBookListRels()) );
$this->assertEquals(1, count(BookListRelPeer::doSelect(new Criteria())) );

}

public function testManyToManyGetterExists()
Expand Down Expand Up @@ -967,4 +966,35 @@ public function testRefIsOnlySavedWhenRequired()
$book->setTitle('Propel2 Book');
$book->save();
}

public function testAddAfterRemoveKeepsReferences()
{
$list = new BookClubList();
$list->setGroupLeader('Archimedes Q. Porter');

$book = new Book();
$book->setTitle( "Jungle Expedition Handbook" );
$book->setIsbn('TEST');

$xref = new BookListRel();
$xref->setBook($book);
$xref->setBookClubList($list);
$xref->save();

$book->removeBookListRel($xref);
$book->addBookListRel($xref);
$book->save();

$this->assertCount(1, $list->getBookListRels());
$this->assertCount(1, $book->getBookListRels());
$this->assertCount(1, BookListRelPeer::doSelect(new Criteria()));

$book->removeBookClubList($list);
$book->addBookClubList($list);
$book->save();

$this->assertCount(1, $list->getBookListRels());
$this->assertCount(1, $book->getBookListRels());
$this->assertCount(1, BookListRelPeer::doSelect(new Criteria()));
}
}

0 comments on commit a5226c9

Please sign in to comment.