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

Conversation

havvg
Copy link
Member

@havvg havvg commented Apr 11, 2013

see #636, #603

@havvg
Copy link
Member Author

havvg commented Apr 11, 2013

@marcj What do you say? You added most of the tests regarding #603.

I wrote down any combination I could come up with, and as far as I can see, the only configuration determining whether to delete or save the related object(s) is required="true|false". This configures whether it's actually valid to save the related one with the null value or not. If it's not valid, the related object has to be deleted as well and this should be done by the ORM instead of CASCADE by the database, e.g. $user->delete() does not actually delete but archive / soft delete it.

The altered test case in @bacf915 fails without the change of @145c0a2.

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Hm, what happens to foreign keys where the local columns are not required? I believe they won't be deleted now with onDelete="CASCADE", right?

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

I just wrote a test and it passes. While writing it, I realized it's plain wrong, because the related object gets deleted - but not by Propel itself, but the database directly because of the CASCADE. Update incoming :)

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

I have no idea, how I could test this behavior, because of the Query::create static invocation.

@marcj
Copy link
Member

marcj commented Apr 19, 2013

You can use SQLite to check that, since we've not implemented there foreign keys yet.

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

Well, then I can't check the foreign keys, can I? (see @f317ce2)
The test case would fail as soon as the FK get implemented, which only delegates the issue to future us :)

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Well, the actual mapping from the schema.xml files are still there, even though the SQLite platform just ignores it in the migration process. Means, $refFK should still contain the reference to a foreign key statement defined in the xml or did I miss something?

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

I will give it a try.

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

The PropelQuickBuilder is using sqlite by default, I added this testcase:

    public function testDeleteCascade()
    {
        \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->delete();

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

However, it passes with and without the changes of @f317ce2. Am I missing something here?

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Have you flagged the columns of the foreign key as required?

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

I thought they should be not required but cascade? Null is valid, but they should be deleted.

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

I tried it in our project, the mentioned changes are indeed required and result in the expected generated code, but still no valid test case to verify this. :(

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Yes, they should not be required to test that, that's why I ask. :)

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Found it, lol. Totally forgot that Peer does it magic too!
Screen Shot 2013-04-19 at 3 50 19 PM

@marcj
Copy link
Member

marcj commented Apr 19, 2013

Ok, a test like that shows the issue with having no ForeignKey::CASCADE === $refFK->getOnDelete() check. It's because ->delete() triggers the peer hooks whereas ->save() does not.

    public function testOnDelete(){
        \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());
    }

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

But you are not deleting the page.

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

Ah wait, I see what you are doing there, thank you!

An object that is related to another one where this relation is required is meant to be deleted by Propel to ensure its behavior upon deletion.
If the relation is not required, but the foreign key defines a CASCADE behavior on deletion, the object will also be deleted.
@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

Added, rebased, squashed.

@marcj
Copy link
Member

marcj commented Apr 19, 2013

The only thing now is that https://github.com/propelorm/Propel/pull/656/files#L0R4317 makes the only change in the test result of GeneratedObjectMoreRelationTest.php. The other lines changed in generator/lib/builder/om/PHP5ObjectBuilder.php generate no difference. Can you explain why do you surround the addScheduledForDeletionAttribute calls with those conditions?

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

Because the attribute is not used, but only generated. Therefore I added the same condition where required, see https://github.com/havvg/Propel/blob/3bf0689decf37195148e948ca86151b9bc470e3e/generator/lib/builder/om/PHP5ObjectBuilder.php#L4790

@marcj
Copy link
Member

marcj commented Apr 19, 2013

ok, perfect! nice job 👯
I'd say: ready to merge

@havvg
Copy link
Member Author

havvg commented Apr 19, 2013

Cool, thanks for the review!

Ping @willdurand

@staabm
Copy link
Member

staabm commented Apr 19, 2013

great job guys! 👍

willdurand added a commit that referenced this pull request Apr 23, 2013
@willdurand willdurand merged commit f6813a0 into propelorm:master Apr 23, 2013
@willdurand
Copy link
Contributor

thx guys!

@havvg havvg deleted the feature/delete-object branch May 13, 2013 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants