public static function findEvents($entityManager, $date) { error_log("date: " . $date->format('Y-m-d')); $query = $entityManager->createQuery('SELECT count(e) FROM Event e WHERE e.date LIKE :date'); $query->setParameter("date", "%" . $date->format('m-d')); $eventNumber = $query->getSingleScalarResult(); if ($eventNumber) { error_log("events for " . $date->format('Y-m-d') . " already exist."); return "1"; } $dim = new \ThisDayIn\Music($date->format('j'), $date->format('F')); $evs = $dim->getEvents(); foreach ($evs as $ev) { $date = new \DateTime($ev['date']); if ($ev['type'] === 'Death') { $ev['description'] = sprintf('%s, %s', $ev['name'], $ev['description']); } //unlike the death events, the birth events do not include enough information in the description. if ($ev['type'] === 'Birth') { $ev['description'] = sprintf('%s, %s was born', $ev['name'], $ev['description']); } //must find artist name for these kind of events if ($ev['type'] == 'Event') { $artist = self::findEventArtist($ev['description']); $ev['name'] = $artist['name']; if (isset($artist['spotifyId'])) { $ev['spotifyId'] = $artist['spotifyId']; } } #TODO: find artist spotify id for the other event types //set current event $event = new \Event(); $event->setDate($date); $event->setDescription($ev['description']); $event->setType($ev['type']); $event->setSource($dim->getSource()); //connects the event to an artist if ($ev['name']) { $artist = $entityManager->getRepository('Artist')->findBy(array('name' => $ev['name'])); $artist = array_shift($artist); if (!$artist) { $artist = new \Artist(); $artist->setName($ev['name']); if (isset($ev['spotifyId'])) { $artist->setSpotifyId($ev['spotifyId']); } } error_log("artist name: " . $artist->getName()); $event->setArtist($artist); $entityManager->persist($event); $artist->assignToEvent($event); $entityManager->persist($artist); #one must save here so it copes for repeated artist in the event list $entityManager->flush(); } $entityManager->persist($event); } //insert all events to db if (count($evs)) { $entityManager->flush(); } return 0; }