Skip to content

Beforeerase function is not triggered #105

@adkurz

Description

@adkurz

I have some tables with 1-n-relations. To delete them without any foreign key problems, al these models have onerase in their constructors.
Example:

Model Category:
    $this->beforeerase(
      function($self) {
        // Delete all items of this category if there is one:
        $item = new Item();
        $item->erase(['category_id = ?', $self->_id]);
        return true;
      }
    );

Model Item:

  public function __construct() {
    parent::__construct();
    // before erase function, delete all contracts of the deleted item:
    $this->beforeerase(
      function($self) {
          // Delete all contracts of this item
        $contract = new Contract();
        $contract->erase(['objekt_id = ?', $self->_id]);
        return true;
      }
    );

If I now want to erase a category, like $category->load(...), $category->erase(), The onerase function of the category-model is triggered, but not the onerase function of the items.
If I use a foreach loop instead, it works, but is slow:

    $this->beforeerase(
      function($self) {
        // Delete all items of this category if there is one:
        $item = new Item();
        $items = $item->find(['kategorie_id = ?', $self->_id]);
        foreach ($items as $value) {
          $value->erase();
        } // foreach
        return true;
      }
    );

Could you please fix this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions