Symfony 4 - Load YML file in fixtures

1.3k views Asked by At

i would like some help about a specific topic on Symfony 4.1. I'd like to load some data stored in a YML file to use fixtures. In order to load this file, I used dependencies injections but I don't understand why my load function is not reached.

This is my code below :

AppExtension :

    <?php
    namespace App\DependencyInjection;

    use Symfony\Component\Config\FileLocator;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\DependencyInjection\Loader;
    use Symfony\Component\DependencyInjection\Extension\Extension;

    /**
     */
    class AppExtension extends Extension
    {
        /**
         * @see Symfony\Component\DependencyInjection\Extension.ExtensionInterface::load()
         */
        public function load(array $configs, ContainerBuilder $container)
        {
            $configuration = new Configuration();
            $config = $this->processConfiguration($configuration, $configs);

            $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
            $loader->load('services.yml');
            //$loader->load('parameters.yml');

            echo __DIR__.'/../Resources/DataFixtures';
            die();

            $fixture_loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/DataFixtures'));
            $fixture_loader->load('Patients.yml');

            $data_loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/datas'));
            //$data_loader->load('first_name.yml')
        }
    }

Configuration.php :

<?php

namespace App\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files.
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app');

        // Here you should define the parameters that are allowed to
        // configure your bundle. See the documentation linked above for
        // more information on that topic.

        return $treeBuilder;
    }
}

Regarding fixtures :

<?php
namespace App\DataFixtures\ORM;

use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\Patient;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Symfony\Component\DependencyInjection\ContainerInterface;


class LoadPatientData extends Fixture {
    /**
     *
     * @var ContainerInterface
     */
    private $container;


    public function __construct(ContainerInterface $container = null)
    {
        $this->container = $container;
    }

    public function load(ObjectManager $manager)
    {
        //var_dump($this->container)

        $patientsArray = $this->container->getParameter('Patient');

        foreach( $patientsArray as $name => $object )
        {
            $patient = new Patient();

            foreach( $object as $key => $value )
            {
                $patient->{$key}($value);
            }

            $manager->persist($patient);
            $this->addReference($name, $patient);
        }
        $manager->flush();
    }

}

Here is the folders tree : folders tree

Console error : Console error

Thanks in advance for your help.

0

There are 0 answers