Load different Data Fixtures depending on configuration in Symfony 3

断了今生、忘了曾经 提交于 2019-12-25 09:28:01

问题


At the moment we are developing a pack of microservices for a few online-shops (and possible future new ones). For business reasons we will have different implementations of the same software, so each shop will ask to its one.

I need to set a different pack of initial data on the database depending on the implementation, which means different Data Fixtures for each one.

I'm looking for a good way to load the proper fixtures depending on configuration parameters (company_name, company_slug...). What would be the best way to do it?


回答1:


if you are using the doctrine/doctrine-fixtures-bundle you could use params in your file params.yml then get it in the load function the container. Something like this

//parameters.yml
parameters
     config: "Cloud" //it can be anyelse

And in the fixturesLoad file

public function setContainer(ContainerInterface $container = null)
{
    var_dump('getting container here');
    $this->container = $container;
}

/**
 * {@inheritDoc}
 */
public function load(ObjectManager $manager)
{
    $config = $this->container->getParameter('config'); //"Cloud"
    if($config == "Cloud"){
         //Do something
    }else{
         //Do something else
    }
}

In my opinion this is the better way to do it, because parameters.yml changes in ever implemetation, and you only need changes it in each enviroment.




回答2:


It can be done by storing the data in a custom parameters file.

Using that global parameter called "company_slug" as a key for the datafixtures pack as follows:

parameters:
    datafixtures:
        # Company 1 datafixtures
        company1:
            defaultusers:
                0:
                    name: john
                    email: john@company1.lol
                1:
                    name: steve
                    email: steve@company1.lol
        # Company 2 datafixtures
        company2:
            defaultusers:
                0:
                    name: anna
                    email: anna@company2.lol
                1:
                    name: eva
                    email: eva@company2.lol

And then develop the data fixtures using this parameters:

public function load(ObjectManager $manager)
{
    $companySlug = $this->container->getParameter('company_slug');
    if (array_key_exists($companySlug, $this->container->getParameter('datafixtures'))) {
        $dataFixtures = $this->container->getParameter('datafixtures')[$companySlug];
    } else {
        throw $this->createException('No datafixtures parameters found for the company slug '.$companySlug);
    }

    foreach ($dataFixtures['defaultusers'] as $u) {
        $user = new User();
        $user->setUserName($u['name']);
        $user->setEmail($u['email']);
        $manager->persist($user);
        $manager->flush();
    }
}


来源:https://stackoverflow.com/questions/44681885/load-different-data-fixtures-depending-on-configuration-in-symfony-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!