public function schedule() { $licenseRepo = $this->em->getRepository('AppBundle:License'); $drillSchemaEventsRepo = $this->em->getRepository('AppBundle:DrillSchemaEvent'); $licensesWithoutSchema = $licenseRepo->findWithoutRegisteredSchema(); foreach ($licensesWithoutSchema as $license) { $drillSchemaEvents = $drillSchemaEventsRepo->findByAddonKey($license->getAddonKey()); if (empty($drillSchemaEvents)) { continue; } $drillRegisteredSchema = new DrillRegisteredSchema(); $drillRegisteredSchema->setLicenseId($license->getLicenseId()); $drillRegisteredSchema->setAddonKey($license->getAddonKey()); $this->em->persist($drillRegisteredSchema); foreach ($drillSchemaEvents as $drillSchemaEvent) { $sendDate = $this->calculateSendDate($drillSchemaEvent, $license); $today = new \DateTime(); // prevent creating events from past if ($sendDate < $today->modify('-2 days')) { continue; } $drillRegisteredEvent = new DrillRegisteredEvent(); $drillRegisteredEvent->setDrillRegisteredSchema($drillRegisteredSchema); $drillRegisteredEvent->setDrillSchemaEvent($drillSchemaEvent); // Calculate $drillRegisteredEvent->setSendDate($sendDate); $drillRegisteredEvent->setStatus('new'); $this->em->persist($drillRegisteredEvent); } } $this->em->flush(); }