Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #603 #604

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is never assigned


$script .= "
if (\$this->{$lowerRelatedName}ScheduledForDeletion !== null) {
if (!\$this->{$lowerRelatedName}ScheduledForDeletion->isEmpty()) {";
if (!$refFK->isComposite() && !$localColumn->isNotNull()) {
if (!$refFK->isForeignPrimaryKey() || $refFK->getOnDelete() == ForeignKey::SETNULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm seems the nonNull check which I commented a few lines above is missing here... maybe missed while refactoring?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's ok. it's just wrong to depend on the first local column, since the whole fk may consists of multiple cols. The other line above is indeed a copy&paste issue. :D But however, it's marked as WIP, so please no comments until it's done anymore ✌️

//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
@@ -0,0 +1,158 @@
<?php

/*
* $Id$
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

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

/**
* Setup schema und some default data
*/
public function setUp()
{
parent::setUp();

if (!class_exists('MoreRelationTest\Page')) {
$schema = <<<EOF
<database name="more_relation_test" namespace="MoreRelationTest">

<table name="more_relation_test_page" phpName="Page">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="title" type="VARCHAR" size="100" primaryString="true" />
</table>
<table name="more_relation_test_content" phpName="Content">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="title" type="VARCHAR" size="100" />
<column name="content" type="LONGVARCHAR" required="false" />
<column name="page_id" type="INTEGER" required="false" />
<foreign-key foreignTable="more_relation_test_page" onDelete="cascade">
<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;

$builder = new PropelQuickBuilder();
$builder->setSchema($schema);
$builder->build();
}

\MoreRelationTest\PagePeer::doDeleteAll();
\MoreRelationTest\ContentPeer::doDeleteAll();

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

$page = new \MoreRelationTest\Page();

$page->setTitle('Page '.$i);
for($j=1;$j<=3;$j++){

$content = new \MoreRelationTest\Content();
$content->setTitle('Content '.$j);
$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 the 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(){


$contentCollection = new PropelObjectCollection();
$contentCollection->setModel('MoreRelationTest\\Content');

$content = new \MoreRelationTest\Content();
$content->setTitle('I should be alone :-(');

$contentCollection[] = $content;

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

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


$page->setContents($contentCollection);
$page->save();

unset($page);

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

}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless blank lines

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, not done yet.






}