k0p*_*kus 14 php yaml fixtures symfony
我想为我的symfony2项目设置灯具.我想避免PHP类,但使用yaml文件来定义fixture.仅存储文本字段和关系的实体工作正常,但我不知道是否可以通过这种方式添加UploadedFile,例如图像文件.
目前,我正在使用KhepinYamlFixtureBundle,并且不确定是否可以通过服务调用定义它们,或者它是否完全没有此功能.
我会切换到提供该功能的捆绑包.
你应该使用爱丽丝。
Alice 是一个 PHP 夹具生成器,允许您轻松地从 PHP 或 Yaml文件加载夹具并管理上传的文件。下面是从 Doctrine Fixtures 类加载一些数据固定装置的代码片段:
<?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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的数据中加载 YML 文件:
# users.yml
MyProject\User:
user_1:
firstName: "Albert"
lastName: "Einstein"
username: "albert"
email: "albert.einstein@protonmail.com"
Run Code Online (Sandbox Code Playgroud)
更多信息请点击这里。
| 归档时间: |
|
| 查看次数: |
1580 次 |
| 最近记录: |