开发者

Doctrine 2 : PreRemove Cascaded On All Entities

I was reading the Doctrine 2 Docs > Events > PreRemove.

The preRemove event is called on every entity when its passed to the EntityManager#remove() method. It is cascaded for all associations that are marked as cascade delete.

There are no restrictions to what methods can be called inside the preRemove event, except when the remove method itself was called during a flush operation.

I am trying to handle what happens to related entities.

eg. Users can ...

  • Own Projects
  • Collaborate in projects
  • Own TodoLists
  • Own Todos
  • Be assigned Todos

So I want to handle all these relationships preRemove. So I did something like

/**
 * @PreRemove
 */
protected function onPreRemove() {
  foreach ($this->projects as $proj) {
    $proj->collaborators->remove($this);
  }
  foreach ($this->ownedTodoLists as $todoList) {
    $todoList->owner = $todoList->project->owner;
  }
  foreach ($this->ownedTodos as $todo) {
    $todo->owner 开发者_开发知识库= $todo->todoList->owner;
  }
  foreach ($this->assignedTodos as $todo) {
    $todo->assigned = $todo->todoList->owner;
  }
}

But I am thinking if this function (preRemove) is called for every association marked cascade delete, if my Todo, TodoList, Links are marked cascade delete, this function call won't be for them? It will waste resources and even throw an error right? $this will refer to entities like Todo when its run on them? How then is a good way to handle these relationships?

Or maybe I don't mark entities cascade but use functions like these to cascade delete manually?


When dealing with special logic (as in your provided snippet), I think this would be a pretty typical usage for the @preRemove event hook. I wouldn't use the internal cascade for any of the columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜