Skip to content

Instantly share code, notes, and snippets.

@jmather
Created January 19, 2013 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmather/4570840 to your computer and use it in GitHub Desktop.
Save jmather/4570840 to your computer and use it in GitHub Desktop.
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