getEntityManager()->getRepository('EmailAddress'); $address = $entity->get($type); $ids = array(); if (!empty($address) || !filter_var($address, FILTER_VALIDATE_EMAIL)) { $arr = array_map(function ($e) { return trim($e); }, explode(';', $address)); $ids = $eaRepositoty->getIds($arr); } $entity->set($type . 'EmailAddressesIds', $ids); } protected function beforeSave(Entity $entity) { $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress'); $from = trim($entity->get('from')); if (!empty($from)) { $ids = $eaRepositoty->getIds(array($from)); if (!empty($ids)) { $entity->set('fromEmailAddressId', $ids[0]); } } else { $entity->set('fromEmailAddressId', null); } $this->prepareAddressess($entity, 'to'); $this->prepareAddressess($entity, 'cc'); $this->prepareAddressess($entity, 'bcc'); parent::beforeSave($entity); $parentId = $entity->get('parentId'); $parentType = $entity->get('parentType'); if (!empty($parentId) || !empty($parentType)) { $parent = $this->getEntityManager()->getEntity($parentType, $parentId); if (!empty($parent)) { if ($parent->getEntityName() == 'Account') { $accountId = $parent->id; } else if ($parent->has('accountId')) { $accountId = $parent->get('accountId'); } if (!empty($accountId)) { $entity->set('accountId', $accountId); } } } else { // TODO find account by from address } } protected function beforeRemove(Entity $entity) { parent::beforeRemove($entity); $attachments = $entity->get('attachments'); foreach ($attachments as $attachment) { $this->getEntityManager()->removeEntity($attachment); } } }