Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm Observers and cascade_delete = true
  • Hello everyone !
    I want to know if there is a way to call nested model delete_before observer before delete entry with has_many relation and cascade_delete = true ?

    I need this to build a path to a file. :/
  • What do you mean by nested?

    Observers cascade as well, so if you have parent->child->grandchild, and the grandchild has an observer and all relations have cascade_delete, the grandchild observer will be called when you delete the parent (which deletes the children which deletes the children of those children).
  • Hello Harro !
    I want to say related model sorry...

    This thing work, yes but I need to delete parent entry after related entries because I need parent to build the path to files to delete them from server (I don't know if it's clear... :/).

    So in related model I have a custom observer with before_delete like this :

    class \Messages\Model\Observer\Attachments
    public function before_delete(\Messages\Model\Attachment $model)
    {
    $conversation = $model->wf_msg_conversations;

    \File::delete('modules/messages'.DS.$conversation->name.'_'.$conversation->id.DS.$model->name);
    }
    Here Attachment is the related model and Conversation the "parent" model.

  • I found this Topic that explain how to modify Orm behavior to delete children before parent in has many relation : https://fuelphp.com/forums/discussion/13441/orm-cascade-delete-order

    It's exactly I want to do but now I get this error :

    Orm\FrozenObject [ Error ]:
    No changes allowed.



    PKGPATH/orm/classes/model.php @ line 962



    957        }
    958        elseif (is_array($rels))
    959        {
    960            if ($this->_frozen)
    961            {
    962                throw new FrozenObject('No changes allowed.');
    963            }
    964            $this->_data_relations $rels;
    965        }
    966        else
    967        {

    I don't know why my Ogject is frozen... :/
  • For the moment, because parent is deleted before chidren (it's strange behavior...), I create another Observer before_delete for Conversation model where I delete files and in Attachment Model Observer before_delete method I only delete file if Conversation parent exists.

    I think think that this is the way to go if I don't want to modify Orm code, right ?
  • Normally, the children are deleted before the parents, if has to do it that way if you have added foreign key constraints to the database, which would also throw an database integrity error.

    So if you do $parent->delete() it should delete the children first. Doesn't it do that? Enable the profiler in your config.php, and enable database profiling in your db.php, to check in what order the SQL is generated.

Howdy, Stranger!

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

In this Discussion