1

My website has two channels: pet_owners and pets. pets channel also has custom field named owner_id.

My problem is: whenever an entry from pet_owners is deleted, would it be possible to force certain entries from pets to be deleted as well?

For example: if an entry from pet_owner gets deleted and its entry_id was 55 then how to automatically delete those pet channel entries where the value of owner_id would also be 55?

How to make it happen? Some plugin or module perhaps?

Thank you.

(P.S. The version of my EE is 2.5.5. Don't know if this information is important or not.)

1
  • 2
    Is there a reason you aren't using actual relationships for this data? You'd still need a custom implementation to automatically delete children when a parent is deleted, but it seems like it would be easier to work with, more future proof, and also more powerful in terms of what you can do with the data on the front end. Apr 15, 2015 at 18:29

1 Answer 1

0

A quick search around didn't find any current third party modules to do this. What you're asking for would require a very small extension to be written, it would take well under an hour for a developer who knows EE. You'd just need an extension hooked to the

delete_entries_loop($val, $channel_id)

extension hook and use something like this:

function delete_pets($val, $channel_id)
{
    // look up field id
    $field_num = ee()->db->select('field_id')
                         ->where('field_name', 'owner_id')
                         ->get()
                         ->row_array();

    $field_id = 'field_id_' . $field_num['field_id'];

    // look up your entries with that field matching this entry
    $entry_ids = ee()->db->select('entry_id')
                         ->from('channel_data')
                         ->where($field_id, $val);
                         ->get()
                         ->result_array();

    // forgot to map this baby down
    $ids = array_map(function($a) { return $a['entry_id']; }, $entry_ids);

    // kill those entries
    ee()->api_channel_entries->delete_entry($ids);
}
1
  • 1
    Your code sample has a typo, but otherwise it works. Thank you. Apr 15, 2015 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.