Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Problem with cascade delete
  • Hi, 

    I have 3 models.

    Quiz HAS MANY Question
    Question BELONGS TO Quiz and HAS MANY Reponses
    Reponse BELONGS TO Question

    In the edit treatment, i put all Questions of the Quiz in $oldQuestions
    I do some code, and i put all new Questions in the Quiz

    If ($quiz->save()) work, i want delete $oldQuestions because they are orphelans

    All old Questions are deleted, but not their Reponses. I have set "cascade_delete" => true in the relationship.

    Here is the code :

    Model_Question : http://bin.fuelphp.com/~hY

    The edit action : http://bin.fuelphp.com/~h1
  • cascade delete only runs a delete on fetched relations, it iterates over the objects and calls delete() on them, it does not run a delete query.

    So if you want to delete responses too, make sure they are fetched:

    $quiz = Model_Quiz::find($id, array('related' => array('question', 'question.response')));
    $quiz->delete();

    This has recently been changed in 1.7: https://github.com/fuel/orm/commit/dcc815e06a5a319154b9c16bb059276d2a1266d2

    If you're on 1.6, you might want to backport this fix.
  • Ok, it's work fine, thanks

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion