How do I delete rows based on multiple fields using entities?

I would like to know how I delete rows based on both the id of the record and also another field, let’s say, email address; in this case, the user’s email.

$contactsTable= TableRegistry::getTableLocator()->get('Contacts');
$contactEntity=$contactsTable->get($Id);
$contactsTable->delete($contactEntity);

This successfully deletes the row by its id, but the problem is that I also need to delete the row based on the user’s email.

My contacts table:

id 
name
email 
phone
address 
user_email

In this case, the user_email is the email of the user who saved the contact and I would like to only delete that contact when both id and the user email match.

How do I do it using entities? I took a look at the documentation but I couldn’t find anythiing regarding that.

Deleting Data - 4.x

Thank you in advance.

What you’re looking for is not really a different way to delete, but a different way to find the entity that you want to delete. You’ll need to use $contactsTable->find()->where([....])->first(). This will return null if there’s no match, or the first record otherwise (presumably, you can have only one match; if there can be multiples, then you’ll need to decide how to handle that).

1 Like

Now that makes sense. Haha, thank you.