getServiceFactory()->create('InboundEmail'); $collection = $this->getEntityManager()->getRepository('InboundEmail')->where(array( 'status' => 'Active', 'useImap' => true ))->find(); foreach ($collection as $entity) { try { $service->fetchFromMailServer($entity); } catch (\Exception $e) { $GLOBALS['log']->error('Job CheckInboundEmails '.$entity->id.': [' . $e->getCode() . '] ' .$e->getMessage()); } } return true; } }