Một giải pháp thay thế cho các đồ đạc bị vỡ theo thư mục là sử dụng lớp lịch thi đấu tùy chỉnh. lớp học cố định của bạn sau đó sẽ mở rộng lớp này và chỉ định các môi trường nó thực sự sẽ được nạp trong
<?php
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides support for environment specific fixtures.
*
* This container aware, abstract data fixture is used to only allow loading in
* specific environments. The environments the data fixture will be loaded in is
* determined by the list of environment names returned by `getEnvironments()`.
*
* > The fixture will still be shown as having been loaded by the Doctrine
* > command, `doctrine:fixtures:load`, despite not having been actually
* > loaded.
*
* @author Kevin Herrera <[email protected]>
*/
abstract class AbstractDataFixture implements ContainerAwareInterface, FixtureInterface
{
/**
* The dependency injection container.
*
* @var ContainerInterface
*/
protected $container;
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
/** @var KernelInterface $kernel */
$kernel = $this->container->get('kernel');
if (in_array($kernel->getEnvironment(), $this->getEnvironments())) {
$this->doLoad($manager);
}
}
/**
* {@inheritDoc}
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* Performs the actual fixtures loading.
*
* @see \Doctrine\Common\DataFixtures\FixtureInterface::load()
*
* @param ObjectManager $manager The object manager.
*/
abstract protected function doLoad(ObjectManager $manager);
/**
* Returns the environments the fixtures may be loaded in.
*
* @return array The name of the environments.
*/
abstract protected function getEnvironments();
}
đồ đạc của bạn sẽ kết thúc lên trông như thế này:.
<?php
namespace Vendor\Bundle\ExampleBundle\DataFixtures\ORM;
use AbstractDataFixture;
use Doctrine\Common\Persistence\ObjectManager;
/**
* Loads data only on "prod".
*/
class ExampleData extends AbstractDataFixture
{
/**
* @override
*/
protected function doLoad(ObjectManager $manager)
{
// ... snip ...
}
/**
* @override
*/
protected function getEnvironments()
{
return array('prod');
}
}
Tôi tin rằng điều này sẽ làm việc với cả hai ORM một thiết bị dữ liệu ODM.
Nguồn
2014-01-16 18:14:38
đây là thiên tài! – ferdynator
Làm cách nào để xác định môi trường từ bảng điều khiển? Sử dụng 'php app/console fixture: load --env = prod'? – xDaizu
Trả lời tự: Yesh, 'php app/console fixture: load --env = prod' hoạt động với giải pháp được cung cấp :) – xDaizu