//Delete relations if any from recurring entity tables before inserting new relations for this entity id
if($params['parent_event_id']){
+ $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesForParent($params['parent_event_id'], 'civicrm_event', FALSE);
+ $implodeRelatedEntities = implode(',', array_map(function($entity){
+ return $entity['id'];
+ }, $getRelatedEntities));
+ //Check if participants exists for events
+ if($implodeRelatedEntities){
+ $query = "SELECT event_id, COUNT(*) as count
+ FROM civicrm_participant
+ WHERE event_id IN ({$implodeRelatedEntities})
+ GROUP BY event_id";
+ $dao = CRM_Core_DAO::executeQuery($query);
+ $participantDetails = array();
+ while($dao->fetch()) {
+ $participantDetails[$dao->event_id] = $dao->count;
+ }
+ }
+ foreach ($getRelatedEntities as $key => $value) {
+ if(!CRM_Utils_Array::value($value['id'], $participantDetails)){
+ CRM_Event_BAO_Event::del($value['id']);
+ }
+ }
CRM_Core_BAO_RecurringEntity::delEntityRelations($params['parent_event_id'], 'civicrm_event');
}