overloading entity delete

Hi all,

sorry if this was already debated.

This is my need: I need to keep a separate database synchronised with Elgg's one.

For this, I overloaded save() in my entity classes, so that is calls insert functions in the other database. Works fine.

For delete, OTOH, I can't overload delete(), because it is often not called: there is an available function delete_entity(), often used in place of the class method. This function does not call the class method, so that the overload would be useless (in fact, the class method calls the function, which personaly I think is a bad idea precisely because of this kind of overloading).

I read the code, and came up with this solution: I overload clearMetaData() in case of empty parameter, because it's a class method called in delete_entity(). Here's some code:

    public function clearMetaData($name = "") {
        if (empty($name)) {
            // we are removing everything about this, so very probably removing this
        return super :: clearMetaData($name);

Any comments or advice? or reference to some resource here or on the web?


  • Elgg *should* call the entity method delete() whenever an entity is deleted. If it is not, it's a bug and you can open a ticket for this at http://trac.elgg.org/

    The function delete_entity() should go away in Elgg 1.9. The original developers tended to write a lot of functions and wrap methods around them. That will be changed in future versions.

  • lib/entities.php does !
    function delete_entity($guid, $recursive = true)
    and <entity>->delete

    ps: what might the (design) replacement for delete_entity() be @1.9 ?


  • Update, because in the meantime I've been working. :-)

    (questions included at the end)

    (side note: I of course totally agree with doing away with functions wrapped in methods, as I demonstrate here... I'm amused by the "original developers" reference... Legacy, you said legacy? :-) )

    to answer @Cash: it's true, entity->delete() should always be called. Let me reformulate: there is a non-null risk that delete_entity() is called instead, especially in the scope of the (rather long) plug-in I'm working on, developed by a team. This risk would be absolutely null if...

    1. delete_entity called entity->delete(), and not the other way round
    2. PHP always knew the type of object it's manipulating

    The second point is probably what's blocking me here, too. Theorically, ElggEntity instances loaded from the database should be of the proper subclass thanks to register_entity_type. Unfortunately I've run into cases where obviously PHP doesn't know the exact subclass it's manipulating, and the overloaded methods never get called. I must admit if I had more time I might look into this, but we don't live in a perfect world. :-(

    My current solution to my delete problem is to add an event handler and a static method in my class, which enables me to code all this functionnally instead of OO.

    register_elgg_event_handler('delete', "object", 'myDeleteObject');
    function myDeleteObject($event, $object_type, $object) {
        if ($object->getSubtype() == JrobinssObject::subtype_str) {

    Note: how do you format code in this message board??

    Works fine for the moment, apart from some spaghetti dependencies.

    Question: where do you add event handlers in a plug-in? In start.php?

    Question: if two entities are linked by a relationship (as defined by addRelationship) does deleting one have an effect on the other?

    Thanks for the help, in any case, it's appreciated!

    Oh, and another question: how do you remove delete_entity without breaking existing code? I assume you meant change its implementation to a simple $entity->delete().