private function importNewsletterDemoData() { $this->modelManager = Shopware()->Container()->get('models'); $newsletterGroup = $this->modelManager->find(Group::class, 1); for ($addressAmount = 0; $addressAmount < 25; $addressAmount++) { $address = new Address(); $address->setEmail('test_' . $addressAmount . '@example.com'); $address->setAdded(new \DateTime()); $address->setNewsletterGroup($newsletterGroup); $address->setIsCustomer(false); $this->modelManager->persist($address); } $this->modelManager->flush(); }
/** * @param array $records * @throws \Enlight_Event_Exception * @throws \Exception */ public function write($records) { if (empty($records['default'])) { $message = SnippetsHelper::getNamespace()->get('adapters/newsletter/no_records', 'No newsletter records were found.'); throw new \Exception($message); } $records = Shopware()->Events()->filter('Shopware_Components_SwagImportExport_DbAdapters_CategoriesDbAdapter_Write', $records, ['subject' => $this]); $defaultValues = $this->getDefaultValues(); /** @var EntityRepository $addressRepository */ $addressRepository = $this->manager->getRepository(Address::class); /** @var EntityRepository $groupRepository */ $groupRepository = $this->manager->getRepository(Group::class); /** @var EntityRepository $contactDataRepository */ $contactDataRepository = $this->manager->getRepository(ContactData::class); $count = 0; foreach ($records['default'] as $newsletterData) { try { $count++; $newsletterData = $this->validator->filterEmptyString($newsletterData); $this->validator->checkRequiredFields($newsletterData); $recipient = $addressRepository->findOneBy(['email' => $newsletterData['email']]); if ($recipient instanceof Address && empty($newsletterData['groupName'])) { continue; } if (!$recipient instanceof Address) { $newsletterData = $this->dataManager->setDefaultFieldsForCreate($newsletterData, $defaultValues); $recipient = new Address(); } $this->validator->validate($newsletterData, NewsletterDataType::$mapper); if ($newsletterData['groupName']) { /** @var Group $group */ $group = $groupRepository->findOneBy(['name' => $newsletterData['groupName']]); if (!$group instanceof Group) { $group = new Group(); $group->setName($newsletterData['groupName']); $this->manager->persist($group); $this->manager->flush($group); } $newsletterData['groupId'] = $group->getId(); } // save newsletter address $newsletterAddress = $this->prepareNewsletterAddress($newsletterData); $recipient->fromArray($newsletterAddress); $this->manager->persist($recipient); if ($recipient->getGroupId() !== 0) { // save mail data $contactData = $contactDataRepository->findOneBy(['email' => $newsletterData['email']]); if (!$contactData instanceof ContactData) { $contactData = new ContactData(); $contactData->setAdded(new \DateTime()); $this->manager->persist($contactData); } $contactData->fromArray($newsletterData); } if ($count % 20 === 0) { $this->manager->flush(); } } catch (AdapterException $e) { $message = $e->getMessage(); $this->saveMessage($message); } } $this->manager->flush(); }