Skip to content

Commit

Permalink
Fixed propelorm#603.
Browse files Browse the repository at this point in the history
Added a test set that proves the bug.
  • Loading branch information
MArcJ committed Feb 15, 2013
1 parent 35dfd06 commit 0fe5e3e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
22 changes: 19 additions & 3 deletions generator/lib/builder/om/PHP5ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3793,7 +3793,7 @@ public function get$relCol(\$criteria = null, PropelPDO \$con = null)
";
} // addRefererGet()

protected function addRefFKSet(&$script, $refFK)
protected function addRefFKSet(&$script, ForeignKey $refFK)
{
$relatedName = $this->getRefFKPhpNameAffix($refFK, $plural = true);
$relatedObjectClassName = $this->getRefFKPhpNameAffix($refFK, $plural = false);
Expand Down Expand Up @@ -3824,8 +3824,22 @@ public function set{$relatedName}(PropelCollection \${$inputCollection}, PropelP
{
\${$inputCollection}ToDelete = \$this->get{$relatedName}(new Criteria(), \$con)->diff(\${$inputCollection});
";

if ($refFK->isForeignPrimaryKey() && $refFK->getOnDelete() != ForeignKey::SETNULL){
$script .= "
//since the foreign key is at the same time the PK and can not be null
//we can not just set it to NULL in the lines below. We have to store
//a backup of all values, so we have later all PK values to remove it then.
\$this->{$inputCollection}ScheduledForDeletion = unserialize(serialize(\${$inputCollection}ToDelete));
";
} else {
$script .= "
\$this->{$inputCollection}ScheduledForDeletion = \${$inputCollection}ToDelete;
";
}

$script .= "
foreach (\${$inputCollection}ToDelete as \${$inputCollectionEntry}Removed) {
\${$inputCollectionEntry}Removed->set{$relCol}(null);
}
Expand Down Expand Up @@ -4076,7 +4090,7 @@ protected function addCrossFkScheduledForDeletion(&$script, $refFK, $crossFK)
";
}

protected function addRefFkScheduledForDeletion(&$script, $refFK)
protected function addRefFkScheduledForDeletion(&$script, ForeignKey $refFK)
{
$relatedName = $this->getRefFKPhpNameAffix($refFK, $plural = true);

Expand All @@ -4090,11 +4104,13 @@ protected function addRefFkScheduledForDeletion(&$script, $refFK)
$queryClassName = $this->getNewStubQueryBuilder($refFK->getTable())->getClassname();

$localColumn = $refFK->getLocalColumn();
$localColumn->isNotNull();

$script .= "
if (\$this->{$lowerRelatedName}ScheduledForDeletion !== null) {
if (!\$this->{$lowerRelatedName}ScheduledForDeletion->isEmpty()) {";
if (!$refFK->isComposite() && !$localColumn->isNotNull()) {
if (!$refFK->isForeignPrimaryKey() || $refFK->getOnDelete() == ForeignKey::SETNULL) {
//the pk is not at the same time a fk, so we have all data to remove it safely through ->save().
$script .= "
foreach (\$this->{$lowerRelatedName}ScheduledForDeletion as \${$lowerSingleRelatedName}) {
// need to save related object because we set the relation to null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
*/

/**
* Tests for SortableBehavior class
* Tests for More relations
*
* @author MArc J. Schmidt
* @version $Revision$
* @package generator.misc
* @package generator.builder.om
*/
class MoreRelationTest extends PHPUnit_Framework_TestCase
class GeneratedObjectMoreRelationTest extends PHPUnit_Framework_TestCase
{


/**
* Setup schema und some default data
*/
public function setUp()
{
parent::setUp();
Expand All @@ -41,6 +43,15 @@ public function setUp()
<reference local="page_id" foreign="id"/>
</foreign-key>
</table>
<table name="more_relation_test_comment" phpName="Comment">
<column name="user_id" required="true" primaryKey="true" type="INTEGER" />
<column name="page_id" required="true" primaryKey="true" type="INTEGER" />
<column name="comment" type="VARCHAR" size="100" />
<foreign-key foreignTable="more_relation_test_page" onDelete="cascade">
<reference local="page_id" foreign="id"/>
</foreign-key>
</table>
</database>
EOF;

Expand All @@ -52,7 +63,7 @@ public function setUp()
\MoreRelationTest\PagePeer::doDeleteAll();
\MoreRelationTest\ContentPeer::doDeleteAll();

for($i=1;$i<=5;$i++){
for($i=1;$i<=2;$i++){

$page = new \MoreRelationTest\Page();

Expand All @@ -64,12 +75,53 @@ public function setUp()
$content->setContent(str_repeat('Content', $j));
$page->addContent($content);

$comment = new \MoreRelationTest\Comment();
$comment->setUserId($j);
$comment->setComment(str_repeat('Comment', $j));
$page->addComment($comment);

}
$page->save();
}

}

/**
* Composite PK deletion of a 1-to-n relation through set<RelationName>()
* where the PK is at them same time a FK.
*/
public function testCommentsDeletion(){

$commentCollection = new PropelObjectCollection();
$commentCollection->setModel('MoreRelationTest\\Comment');

$comment = new \MoreRelationTest\Comment();
$comment->setComment('I should be alone :-(');
$comment->setUserId(123);

$commentCollection[] = $comment;

$page = \MoreRelationTest\PageQuery::create()->findOne();
$id = $page->getId();

$count = \MoreRelationTest\CommentQuery::create()->filterByPageId($id)->count();
$this->assertEquals(3, $count, 'We created for each page 3 comments.');


$page->setComments($commentCollection);
$page->save();

unset($page);

$count = \MoreRelationTest\CommentQuery::create()->filterByPageId($id)->count();
$this->assertEquals(1, $count, 'We assigned a collection of only one item.');

}

/**
* Basic deletion of a 1-to-n relation through set<RelationName>().
*
*/
public function testContentsDeletion(){


Expand Down

0 comments on commit 0fe5e3e

Please sign in to comment.