* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ /** * Load data fixtures from bundles. * * @author Fabien Potencier * @author Jonathan H. Wage */ class LoadDataFixturesDoctrineCommand extends DoctrineCommand { protected function configure() { $this ->setName('doctrine:data:load') ->setDescription('Load data fixtures to your database.') ->addOption('fixtures', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, 'The directory or file to load data fixtures from.') ->addOption('append', null, InputOption::PARAMETER_OPTIONAL, 'Whether or not to append the data fixtures.', false) ->addOption('em', null, InputOption::PARAMETER_REQUIRED, 'The entity manager to use for this command.') ->setHelp(<<doctrine:data:load command loads data fixtures from your bundles: ./symfony doctrine:data:load You can also optionally specify the path to fixtures with the --fixtures option: ./symfony doctrine:data:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2 If you want to append the fixtures instead of flushing the database first you can use the --append option: ./symfony doctrine:data:load --append EOT ); } protected function execute(InputInterface $input, OutputInterface $output) { $emName = $input->getOption('em'); $emName = $emName ? $emName : 'default'; $emServiceName = sprintf('doctrine.orm.%s_entity_manager', $emName); $em = $this->container->get($emServiceName); $dirOrFile = $input->getOption('fixtures'); if ($dirOrFile) { $paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); } else { $paths = array(); $bundleDirs = $this->container->getKernelService()->getBundleDirs(); foreach ($this->container->getKernelService()->getBundles() as $bundle) { $tmp = dirname(str_replace('\\', '/', get_class($bundle))); $namespace = str_replace('/', '\\', dirname($tmp)); $class = basename($tmp); if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/DataFixtures/ORM')) { $paths[] = $dir; } } } $loader = new \Doctrine\Common\DataFixtures\Loader(); foreach ($paths as $path) { $loader->loadFromDirectory($path); } $fixtures = $loader->getFixtures(); $purger = new \Doctrine\Common\DataFixtures\Purger\ORMPurger($em); $executor = new \Doctrine\Common\DataFixtures\Executor\ORMExecutor($em, $purger); $executor->execute($fixtures, $input->getOption('append')); } }