问题描述
目前,我们正在为一些在线商店(以及未来可能的新商店)开发一组微服务.出于业务原因,我们将对同一软件采用不同的实现方式,因此每个商店都会要求自己的一个.
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.
我正在寻找一种根据配置参数(company_name、company_slug...)加载正确装置的好方法.最好的方法是什么?
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?
推荐答案
可以通过将数据存储在自定义参数文件中来完成.
It can be done by storing the data in a custom parameters file.
使用名为company_slug"的全局参数作为数据夹具包的键,如下所示:
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();
}
}
这篇关于根据 Symfony 3 中的配置加载不同的数据装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!