Permalink
Browse files

Fixed problem when pk already exists on parent table

  • Loading branch information...
1 parent db47b84 commit d15c3898af7c8facd8fd64560d6592a934850a93 @pablogd committed Apr 16, 2012
@@ -197,22 +197,26 @@ protected function addObjectGetParentOrCreate(&$script)
*/
public function getParentOrCreate(\$con = null)
{
- if (\$this->isNew() && \$this->isPrimaryKeyNull()) {
- //this prevent issue with deep copy & save parent object
- if (null === (\$parent = \$this->get". $parentClass . "(\$con))) {
- \$parent = new " . $parentClass . "();
- }
- \$parent->set" . $this->getParentTable()->getColumn($this->getParameter('descendant_column'))->getPhpName() . "('" . $this->builder->getStubObjectBuilder()->getClassname() . "');
- return \$parent;
- } else {
- \$parent = " . $this->builder->getNewStubQueryBuilder($parentTable)->getClassname() . "::create()->findPk(\$this->getPrimaryKey(), \$con);
- if ( \$parent === null ) {
- \$parent = new " . $parentClass . "();
- \$parent->setPrimaryKey(\$this->getPrimaryKey());
+ if (\$this->isNew()) {
+ if (\$this->isPrimaryKeyNull()) {
+ //this prevent issue with deep copy & save parent object
+ if (null === (\$parent = \$this->get". $parentClass . "(\$con))) {
+ \$parent = new " . $parentClass . "();
+ }
\$parent->set" . $this->getParentTable()->getColumn($this->getParameter('descendant_column'))->getPhpName() . "('" . $this->builder->getStubObjectBuilder()->getClassname() . "');
+ return \$parent;
+ } else {
+ \$parent = " . $this->builder->getNewStubQueryBuilder($parentTable)->getClassname() . "::create()->findPk(\$this->getPrimaryKey(), \$con);
+ if (null === \$parent || null !== \$parent->getDescendantClass()) {
+ \$parent = new " . $parentClass . "();
+ \$parent->setPrimaryKey(\$this->getPrimaryKey());
+ \$parent->set" . $this->getParentTable()->getColumn($this->getParameter('descendant_column'))->getPhpName() . "('" . $this->builder->getStubObjectBuilder()->getClassname() . "');
+ }
+ return \$parent;
}
- return \$parent;
}
+
+ return " . $this->builder->getNewStubQueryBuilder($parentTable)->getClassname() . "::create()->findPk(\$this->getPrimaryKey(), \$con);
}
";
}
@@ -253,5 +253,22 @@ public function testSetPKOnNewObject()
$articledb = ConcreteArticleQuery::create()->findOneById(2);
$this->assertEquals(2, $articledb->getId(), 'getParentOrCreate() keeps manually set pk after save and reload from db');
}
+
+ public function testSetPKOnNewObjectWithPkAlreadyInParentTable()
+ {
+ ConcreteContentQuery::create()->deleteAll();
+ ConcreteArticleQuery::create()->deleteAll();
+ try {
+ $article = new ConcreteArticle();
+ $article->setId(4);
+ $article->save();
+ $article = new ConcreteArticle();
+ $article->setId(4);
+ $article->save();
+ $this->fail('getParentOrCreate() returns a new parent object on new child objects with pk set');
+ } catch (PropelException $e) {
+ $this->assertTrue(true, 'getParentOrCreate() returns a new parent object on new child objects with pk set');
+ }
+ }
}

0 comments on commit d15c389

Please sign in to comment.