$q = new Doctrine_Query(); $q->from('UserImagefile uif'); $q->innerJoin('uif.Imagefile if'); $q->where('if.md5hash = ? AND uif.user_id = ?', array($image_id, $user_id)); $user_imagefile = $q->execute()->getFirst(); if (empty($user_imagefile)) { throw new XZ2Exception(__METHOD__.' - No image found with id: '.$image_id, XZ2Exception::NOT_FOUND); } $user_imagefile->Imagefile->delete();