public function load(ObjectManager $em) { $files = Finder::create()->name('*.*')->in(__DIR__ . '/../data/files'); $manager = $this->container->get('sonata.media.manager.media'); $medias = []; foreach ($files as $pos => $file) { $media = new Media(); $media->setBinaryContent($file); $media->setEnabled(true); $manager->save($media, 'gallery', 'sonata.media.provider.image'); $medias[] = $media; } for ($i = 1; $i < 2; $i++) { $post = new Photo(); $post->setCategory($em->merge($this->getReference("PhotoCategory-1"))); $post->setTitle("testowe " . $i); $post->setImage($medias[0]); $post2 = new Photo(); $post2->setCategory($em->merge($this->getReference("PhotoCategory-" . rand(1, 6)))); $post2->setTitle("testowe " . $i); $post2->setImage($medias[1]); $em->persist($post2); $em->persist($post); $em->flush(); $this->addReference('Photo-' . $i, $post); $this->addReference('Photo-' . ($i - 1), $post2); } }
protected function addPhotosToCategory($message) { $categoryName = $message->getValue('categoryName'); $filePath = $message->getValue('filePath'); $category = $this->enityManager->getRepository('AldorGalleryBundle:PhotoCategory')->findOneByName($categoryName); $zip = new ExtractZip($filePath); $files = $zip->extract(); $counter = 0; $maxSequence = $this->enityManager->getRepository('AldorGalleryBundle:Photo')->getMaxSequenceByCategory($category->getId()); foreach ($files as $pos => $file) { $title = explode('/', $pos); $fileName = $title[count($title) - 1]; $fileName = str_replace('DSC_', '', $fileName); $title = $categoryName . '-' . explode('.', $fileName)[0]; $photo = new Photo(); $photo->setTitle($title); $media = new Media(); $media->setBinaryContent($file); $media->setEnabled(true); $media->setContext('gallery'); $media->setName($categoryName . '-' . $fileName); $photo->setImage($media); $photo->addCategory($category); $photo->setSequence($maxSequence++); $this->enityManager->persist($photo); } $this->enityManager->flush(); $zip->clean(); }