public function buildEntity($value) { $parent = null; $child = null; try { /* @var $parent \Giosh94mhz\GeonamesBundle\Entity\Toponym */ $parent = $this->toponymRepository->find($value[0]); /* @var $child \Giosh94mhz\GeonamesBundle\Entity\Toponym */ $child = $this->toponymRepository->find($value[1]); if (!$parent || !$child) { throw new MissingToponymException("HierarchyLink not imported due to missing toponym '{$value['0']}=>{$value['1']}'"); } /* @var $link \Giosh94mhz\GeonamesBundle\Entity\HierarchyLink */ $link = $this->repository->find(array('parent' => $parent->getId(), 'child' => $child->getId())) ?: new HierarchyLink($parent, $child); $link->setType($value[2]); return $link; } catch (\Exception $e) { if ($parent !== null) { $this->om->detach($parent); } if ($child !== null) { $this->om->detach($child); } throw $e; } }
/** * {@inheritdoc} */ public function get($name, $locale = null, $default = null) { if (null === $locale) { if (null === $this->requests || !($request = $this->requests->getMasterRequest())) { throw new MissingLocaleException('Could not get content because locale is missing. Try passing it as a parameter.'); } $locale = $request->getLocale(); } $content = $this->om->getRepository(Content::class)->findOneBy(['name' => $name, 'locale' => $locale]); if (!$content) { if (null === $default) { $default = $name; } $content = $this->create($name, $default, $locale); } $this->om->detach($content); return $content; }
/** * Build menu. * * @param string $menuCode Menu code * * @return MenuInterface Menu * * @throws Exception */ private function buildMenuFromRepository($menuCode) { $menu = $this->menuRepository->findOneBy(['code' => $menuCode, 'enabled' => true]); if (!$menu instanceof MenuInterface) { throw new Exception(sprintf('Menu "%s" not found', $menuCode)); } $this->menuObjectManager->detach($menu); return $menu; }
/** * Load meta object by provided type and parameters. * * @MetaLoaderDoc( * description="Article Loader loads articles from Content Repository", * parameters={ * contentPath="SINGLE|required content path", * slug="SINGLE|required content slug", * pageName="COLLECTiON|name of Page for required articles" * } * ) * * @param string $type object type * @param array $parameters parameters needed to load required object type * @param int $responseType response type: single meta (LoaderInterface::SINGLE) or collection of metas (LoaderInterface::COLLECTION) * * @return Meta|Meta[]|bool false if meta cannot be loaded, a Meta instance otherwise * * @throws \Exception */ public function load($type, $parameters = [], $responseType = LoaderInterface::SINGLE) { $criteria = new Criteria(); if ($responseType === LoaderInterface::SINGLE) { if (array_key_exists('article', $parameters) && $parameters['article'] instanceof ArticleInterface) { $this->dm->detach($parameters['article']); $criteria->set('id', $parameters['article']->getId()); } elseif (array_key_exists('slug', $parameters)) { $criteria->set('slug', $parameters['slug']); } try { return $this->getArticleMeta($this->articleProvider->getOneByCriteria($criteria)); } catch (NotFoundHttpException $e) { return; } } elseif ($responseType === LoaderInterface::COLLECTION) { $currentPage = $this->context->getCurrentPage(); $route = null; if (null !== $currentPage) { $route = $currentPage->getValues(); } if (array_key_exists('route', $parameters)) { if (null === $route || $route instanceof RouteInterface && $route->getId() !== $parameters['route']) { if (is_int($parameters['route'])) { $route = $this->routeProvider->getOneById($parameters['route']); } elseif (is_string($parameters['route'])) { $route = $this->routeProvider->getOneByStaticPrefix($parameters['route']); } } } if ($route instanceof RouteInterface) { $criteria->set('route', $route); } else { return; } $criteria = $this->applyPaginationToCriteria($criteria, $parameters); $articles = $this->articleProvider->getManyByCriteria($criteria); if ($articles->count() > 0) { $metaCollection = new MetaCollection(); $metaCollection->setTotalItemsCount($this->articleProvider->getCountByCriteria($criteria)); foreach ($articles as $article) { $articleMeta = $this->getArticleMeta($article); if (null !== $articleMeta) { $metaCollection->add($articleMeta); } } unset($articles, $route, $criteria); return $metaCollection; } } return; }
public function load(ObjectManager $manager) { // Retrieve the Faker service $faker = $this->container->get('faker.generator'); // Retrieve the password encoder $encoder = $this->container->get('security.encoder_factory')->getEncoder('KingFoo\\BookmarkerBundle\\Entity\\User'); // Create some tags $labels = array('doctrine', 'symfony', 'twig', 'kingfoo', 'training', 'yaml', 'php', 'mvc'); $tags = array(); foreach ($labels as $label) { $tag = new Tag($label); $manager->persist($tag); $tags[] = $tag; } $manager->flush(); $popular = array('http://symfony.com/', 'http://www.doctrine-project.org/', 'http://twig.sensiolabs.org/', 'http://www.google.com/'); for ($i = 0; $i < 20; $i++) { // Create a new user $user = new User(); $user->setUsername($faker->username)->setEmail($faker->email)->setPassword($encoder->encodePassword($user->getUsername(), $user->getSalt())); $manager->persist($user); // Add a popular bookmark $bookmark = new Bookmark(); $bookmark->setUser($user)->setUrl($faker->randomElement($popular))->setDescription($faker->paragraph)->setCreatedAt($faker->dateTimeThisMonth); // Add some tags shuffle($tags); for ($j = 0; $j < rand(1, count($tags)); $j++) { $tag = $tags[($i + $j) % count($tags)]; $bookmark->addTag($tag); } $manager->persist($bookmark); // Create some random bookmarks for every user for ($j = 0; $j < 50; $j++) { $bookmark = new Bookmark(); $bookmark->setUser($user)->setUrl($faker->url)->setDescription($faker->paragraph)->setCreatedAt($faker->dateTimeThisYear); // Add some tags shuffle($tags); for ($k = 0; $k < rand(1, count($tags)); $k++) { $tag = $tags[($i + $j + $k) % count($tags)]; $bookmark->addTag($tag); } $manager->persist($bookmark); } $manager->flush(); $manager->detach($user); $manager->clear('KingFoo\\BookmarkerBundle\\Entity\\Bookmark'); } }
/** * @param BlockInterface $block */ public function load(BlockInterface $block) { $this->form = $this->formFactory->create(SubscribeType::class, $this->subscription); $this->form->handleRequest($this->request); if ($this->form->isValid()) { foreach ($this->getMailingLists($block) as $mailingList) { $this->subscription->setMailingList($mailingList); $this->em->persist($this->subscription); $this->em->flush($this->subscription); // Reset to add to another mailing list $this->em->detach($this->subscription); $this->subscription = clone $this->subscription; $this->subscription->setId(null); } $this->subscribed = true; } }
/** * @param ResourceInterface $resource */ public function detach(ResourceInterface $resource) { $this->manager->detach($resource); }
/** * @param \Symfony\Component\Security\Core\User\UserInterface $user * @return self */ public function detach(UserInterface $user) { $this->em->detach($user); return $this; }
/** * Insert new users * * @param Collection $users */ private function insertUsers(Collection $users) { set_time_limit(60); $this->manager->getConnection()->getConfiguration()->setSQLLogger(null); // Get existan user from DB $existantUsers = $this->repository->findBy(['email' => $this->extractEmails($users)->toArray()]); // Create an array of emails $existantEmails = array_map(function ($user) { return $user->getEmail(); }, $existantUsers); unset($existantUsers); // Get not existant users ready to import $nonExistantUsers = $users->filter(function ($user) use($existantEmails) { return !in_array($user->getEmail(), $existantEmails); }); unset($existantEmails); foreach ($nonExistantUsers as $user) { $user->addRole('ROLE_USER'); $this->manager->persist($user); $this->manager->flush(); $this->manager->clear(); $this->manager->detach($user); gc_enable(); gc_collect_cycles(); } }