开发者

Proper way to prevent Doctrine record from being deleted

Can I just override delete() method of Doctrine_Record?

class myRec extends myRecBase开发者_Python百科
  public function delete() {
    return false;
  }
}

Or use preDelete hook? Or throw an exception in delete()?

Any help will be appreciated.


One option is to use the preDelete method to simply skip the delete method all together.

Example:

public function preDelete(Doctrine_Event $event)
{
   $event->skipOperation();
}

You should be able to do this within your extended Doctrine_Record classes or, as an alternative, with a listener. A listener may be beneficial in the cases where you want some part of your application to have the ability to delete records (e.g. an admin section vs client side code).

With the listener approach you create a listener with the above preDelete method and register it with your Table like this:

$userTable = Doctrine_Core::getTable('User');
$userTable->addRecordListener(new HydrationListener());

If the listener is not registered you will still be able to delete records. If the listener is registered your deletes will not persist to the database.

More information about that method can be found here: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/en


IMHO I would probably do it that way too as a preDelete hook is still going to execute the delete method afterwards.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜