Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load in file assets
<?php
namespace Demo\Bundle\WebsiteBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Application\Sonata\UserBundle\Entity\User;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Pff\Bundle\CmsBundle\Entity\Page;
use Pff\Bundle\CmsBundle\Entity\SliderImage;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class LoadWebsiteSpecificData extends AbstractFixture implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$kernel = $this->container->get('kernel');
$basedir = $kernel->locateResource('@DemoWebsiteBundle/Resources/demo-sliders').'/';
$files = glob($basedir.'/*.jpeg');
foreach($files as $file) {
$tmpfile = tempnam('/tmp', 'upload');
copy($file, $tmpfile);
$file = new UploadedFile($tmpfile, basename($file), 'image/jpeg', filesize($file), UPLOAD_ERR_OK, true);
$img = new SliderImage();
$img->setImage($file);
$img->setName(basename($file));
$img->setEnabled(true);
$img->setAltText(basename($file));
$img->setUrl('#');
$manager->persist($img);
}
$manager->flush();
$manager->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment