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

deleting objects #656

Merged
merged 1 commit into from Apr 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions generator/lib/builder/om/PHP5ObjectBuilder.php
Expand Up @@ -270,15 +270,22 @@ protected function addClassBody(&$script)

if ($table->hasCrossForeignKeys()) {
foreach ($table->getCrossFks() as $fkList) {
/* @var $refFK ForeignKey */
list($refFK, $crossFK) = $fkList;
$fkName = $this->getFKPhpNameAffix($crossFK, $plural = true);
$this->addScheduledForDeletionAttribute($script, $fkName);

if (!$refFK->isLocalPrimaryKey()) {
$this->addScheduledForDeletionAttribute($script, $fkName);
}
}
}

foreach ($table->getReferrers() as $refFK) {
$fkName = $this->getRefFKPhpNameAffix($refFK, $plural = true);
$this->addScheduledForDeletionAttribute($script, $fkName);

if (!$refFK->isLocalPrimaryKey()) {
$this->addScheduledForDeletionAttribute($script, $fkName);
}
}

if ($this->hasDefaultValues()) {
Expand Down Expand Up @@ -4307,9 +4314,8 @@ protected function addRefFkScheduledForDeletion(&$script, ForeignKey $refFK)
if (\$this->{$lowerRelatedName}ScheduledForDeletion !== null) {
if (!\$this->{$lowerRelatedName}ScheduledForDeletion->isEmpty()) {";

if ($refFK->getOnDelete() == ForeignKey::CASCADE) {
if ($refFK->isLocalColumnsRequired() || ForeignKey::CASCADE === $refFK->getOnDelete()) {
$script .= "
//the foreign key is flagged as `CASCADE`, so we delete the items
$queryClassName::create()
->filterByPrimaryKeys(\$this->{$lowerRelatedName}ScheduledForDeletion->getPrimaryKeys(false))
->delete(\$con);";
Expand Down Expand Up @@ -4781,8 +4787,6 @@ protected function doSave(PropelPDO \$con" . ($reloadOnUpdate || $reloadOnInsert
}

foreach ($table->getReferrers() as $refFK) {
$this->addRefFkScheduledForDeletion($script, $refFK);

if ($refFK->isLocalPrimaryKey()) {
$varName = $this->getPKRefFKVarName($refFK);
$script .= "
Expand All @@ -4793,6 +4797,8 @@ protected function doSave(PropelPDO \$con" . ($reloadOnUpdate || $reloadOnInsert
}
";
} else {
$this->addRefFkScheduledForDeletion($script, $refFK);

$collName = $this->getRefFKCollVarName($refFK);
$script .= "
if (\$this->$collName !== null) {
Expand Down
Expand Up @@ -49,7 +49,7 @@ public function setUp()
<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">
<foreign-key foreignTable="more_relation_test_page" onDelete="restrict">
<reference local="page_id" foreign="id"/>
</foreign-key>
</table>
Expand Down Expand Up @@ -212,4 +212,25 @@ public function testContentsDeletion()

}

public function testOnDeleteCascadeNotRequired()
{
\MoreRelationTest\PagePeer::doDeleteAll();
\MoreRelationTest\ContentPeer::doDeleteAll();

$page = new \MoreRelationTest\Page();
$page->setTitle('Some important Page');

$content = new \MoreRelationTest\Content();
$content->setTitle('Content');

$page->addContent($content);
$page->save();

$this->assertEquals(1, \MoreRelationTest\ContentQuery::create()->count());

$page->removeContent($content);
$page->save();

$this->assertEquals(0, \MoreRelationTest\ContentQuery::create()->count());
}
}