I want to set up fixtures for my symfony2 project. I want to avoid PHP classes but use yaml files to define the fixtures. Entities that only store text fields and relationships work fine, yet I do not know if it is possible to add UploadedFile, e.g. image files, this way.
At the moment, I am using KhepinYamlFixtureBundle and am not sure if it is possible to define them via a service call or if it doesn't have this feature at all.
I would switch to a bundle providing the feature.
You should use Alice.
Alice is a PHP fixtures generator that allows you to load fixtures from PHP or Yaml files easily and manage uploaded files. Here is a snippet of code that loads some data fixtures from a Doctrine Fixtures class:
<?php
namespace MyProject\User\Fixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\Loader\Yaml;
use Nelmio\Alice\ORM\Doctrine;
use Symfony\Component\Finder\Finder;
class LoadUserData implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        // load objects from a yaml file
        $loader = new \Nelmio\Alice\Loader\Yaml();
        $objects = $loader->load(__DIR__.'/users.yml');
        $persister = new \Nelmio\Alice\ORM\Doctrine($manager);
        $persister->persist($objects);
        // Copy images into the legacy application
        $fs = $this->container->get('filesystem');
        $legacyPath = $this->container->getParameter('legacy_path').'/web/uploads';
        $basePath = __DIR__.'/../files';
        $finder = \Symfony\Component\Finder\Finder::create()
            ->files()
            ->in($basePath)
            ->ignoreDotFiles(true)
            ->ignoreVCS(true)
        ;
        foreach ($finder as $file) {
            if ($file->isFile()) {
                $newFile = str_replace($basePath, $legacyPath, $file->getPathname());
                $fs->copy($file->getPathname(), $newFile);
            }
        }
    }
}
And in your data to load in a YML file :
# users.yml
MyProject\User:
    user_1:
        firstName: "Albert"
        lastName:  "Einstein"
        username:  "albert"
        email:     "[email protected]"
More info here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With