Skip to content

Commit

Permalink
Merge pull request propelorm#342 from msarris/master
Browse files Browse the repository at this point in the history
Cross ref setter (generated by addCrossFKSet) should use FK relation name (phpName) in query filter and add methods
  • Loading branch information
willdurand committed Apr 25, 2012
2 parents e769175 + 108a0b7 commit dfd0bd2
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
30 changes: 22 additions & 8 deletions generator/lib/builder/om/PHP5ObjectBuilder.php
Expand Up @@ -3915,10 +3915,10 @@ public function get{$relatedName}(\$criteria = null, PropelPDO \$con = null)

protected function addCrossFKSet(&$script, $refFK, $crossFK)
{
$relatedName = $this->getFKPhpNameAffix($crossFK, $plural = true);
$relatedNamePlural = $this->getFKPhpNameAffix($crossFK, $plural = true);
$relatedName = $this->getFKPhpNameAffix($crossFK, $plural = false);
$relatedObjectClassName = $this->getNewStubObjectBuilder($crossFK->getForeignTable())->getClassname();
$selfRelationName = $this->getFKPhpNameAffix($refFK, $plural = false);
$relatedQueryClassName = $this->getNewStubQueryBuilder($crossFK->getForeignTable())->getClassname();
$crossRefQueryClassName = $this->getRefFKPhpNameAffix($refFK, $plural = false);
$crossRefTableName = $crossFK->getTableName();
$collName = $this->getCrossFKVarName($crossFK);
Expand All @@ -3932,7 +3932,7 @@ protected function addCrossFKSet(&$script, $refFK, $crossFK)
$crossRefObjectClassName = '$' . $lowerClassName;

// No lcfirst() in PHP < 5.3
$inputCollection = $relatedName;
$inputCollection = $relatedNamePlural;
$inputCollection[0] = strtolower($inputCollection[0]);

// No lcfirst() in PHP < 5.3
Expand All @@ -3952,22 +3952,36 @@ protected function addCrossFKSet(&$script, $refFK, $crossFK)
* @param PropelCollection \${$inputCollection} A Propel collection.
* @param PropelPDO \$con Optional connection object
*/
public function set{$relatedName}(PropelCollection \${$inputCollection}, PropelPDO \$con = null)
public function set{$relatedNamePlural}(PropelCollection \${$inputCollection}, PropelPDO \$con = null)
{
{$crossRefObjectClassName}s = {$crossRefQueryClassName}Query::create()
->filterBy{$relatedObjectClassName}(\${$inputCollection})
->filterBy{$relatedName}(\${$inputCollection})
->filterBy{$selfRelationName}(\$this)
->find(\$con);
\$this->{$inputCollection}ScheduledForDeletion = \$this->get{$relCol}()->diff({$crossRefObjectClassName}s);
\$current{$relCol} = \$this->get{$relCol}();
\$this->{$inputCollection}ScheduledForDeletion = \$current{$relCol}->diff({$crossRefObjectClassName}s);
\$this->{$relColVarName} = {$crossRefObjectClassName}s;
foreach (\${$inputCollection} as \${$inputCollectionEntry}) {
// Skip objects that are already in the current collection.
\$isInCurrent = false;
foreach (\$current{$relCol} as {$crossRefObjectClassName}) {
if ({$crossRefObjectClassName}->get{$relatedName}() == \${$inputCollectionEntry}) {
\$isInCurrent = true;
break;
}
}
if (\$isInCurrent) {
continue;
}
// Fix issue with collection modified by reference
if (\${$inputCollectionEntry}->isNew()) {
\$this->doAdd{$relatedObjectClassName}(\${$inputCollectionEntry});
\$this->doAdd{$relatedName}(\${$inputCollectionEntry});
} else {
\$this->add{$relatedObjectClassName}(\${$inputCollectionEntry});
\$this->add{$relatedName}(\${$inputCollectionEntry});
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/bookstore/schema.xml
Expand Up @@ -159,6 +159,18 @@
</foreign-key>
</table>

<table name="book_club_list_favorite_books" phpName="BookListFavorite" isCrossRef="true"
description="Another cross-reference table for many-to-many relationship between book rows and book_club_list rows for favorite books.">
<column name="book_id" primaryKey="true" type="INTEGER" description="Fkey to book.id" />
<column name="book_club_list_id" primaryKey="true" type="INTEGER" description="Fkey to book_club_list.id" />
<foreign-key foreignTable="book" phpName="FavoriteBook" onDelete="cascade">
<reference local="book_id" foreign="id" />
</foreign-key>
<foreign-key foreignTable="book_club_list" phpName="FavoriteBookClubList" onDelete="cascade">
<reference local="book_club_list_id" foreign="id" />
</foreign-key>
</table>

<!-- test self-referencing foreign keys and inheritance-->
<table name="bookstore_employee"
description="Hierarchical table to represent employees of a bookstore.">
Expand Down
Expand Up @@ -41,7 +41,7 @@ public function testColumnDefaultValue()
public function testRelationCount()
{
$bookTable = $this->databaseMap->getTableByPhpName('Book');
$this->assertEquals(10, count($bookTable->getRelations()), 'The map builder creates relations for both incoming and outgoing keys');
$this->assertEquals(12, count($bookTable->getRelations()), 'The map builder creates relations for both incoming and outgoing keys');
}

public function testSimpleRelationName()
Expand Down
29 changes: 28 additions & 1 deletion test/testsuite/misc/BookstoreTest.php
Expand Up @@ -808,6 +808,31 @@ public function testScenarioUsingQuery()
$relCount = $blc2->countBookListRels();
$this->assertEquals(1, $relCount, 'BookClubList 2 has 1 BookListRel');

// Test collection setter for cross reference with custom relation names for the FK's.

$booksCollection = BookQuery::create()->setLimit(4)->find();

$blc3 = new BookClubList();
$blc3->setFavoriteBooks($booksCollection);
$blc3->save();

$blc3->reload(true);

$this->assertEquals(4, $blc3->countBookListFavorites(), "BookClubList has 4 favorites");

// Test set collection with less values, removing the missing and skipping the existing to prevent duplicate key.

$booksCollection = BookQuery::create()->setLimit(2)->find();

$blc3->reload(true);

$blc3->setFavoriteBooks($booksCollection);
$blc3->save();

$blc3->reload(true);

$this->assertEquals(2, $blc3->countBookListFavorites(), "BookClubList has 2 favorites after setting new collection");

// Cleanup (tests DELETE)
// ----------------------

Expand Down Expand Up @@ -858,6 +883,7 @@ public function testScenarioUsingQuery()
$scholastic->delete();
$blc1->delete();
$blc2->delete();
$blc3->delete();

$this->assertEquals(array(), AuthorPeer::doSelect(new Criteria()), 'no records in [author] table');
$this->assertEquals(array(), PublisherPeer::doSelect(new Criteria()), 'no records in [publisher] table');
Expand All @@ -866,5 +892,6 @@ public function testScenarioUsingQuery()
$this->assertEquals(array(), MediaPeer::doSelect(new Criteria()), 'no records in [media] table');
$this->assertEquals(array(), BookClubListPeer::doSelect(new Criteria()), 'no records in [book_club_list] table');
$this->assertEquals(array(), BookListRelPeer::doSelect(new Criteria()), 'no records in [book_x_list] table');
$this->assertEquals(array(), BookListFavoritePeer::doSelect(new Criteria()), 'no records in [book_club_list_favorite_books] table');
}
}
}

0 comments on commit dfd0bd2

Please sign in to comment.