Skip to content

Instantly share code, notes, and snippets.

@akiletour
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiletour/5f09c970bed1ff556220 to your computer and use it in GitHub Desktop.
Save akiletour/5f09c970bed1ff556220 to your computer and use it in GitHub Desktop.
<?php
// src/Acme/DemoBundle/DataFixtures/AbstractDataFixture.php
namespace Acme\DemoBundle\DataFixtures;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
abstract class AbstractDataFixture extends AbstractFixture implements ContainerAwareInterface, FixtureInterface, OrderedFixtureInterface
{
/**
* 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);
} else {
$ouput = new ConsoleOutput();
$msg = '...Not for ' . $kernel->getEnvironment() . ' environment';
$ouput->writeln(' <comment>></comment> <comment>' . $msg . '</comment>');
}
}
/**
* {@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();
}
<?php
// src/Acme/DemoBundle/DataFixtures/ORM/LoadDevUsersData.php
namespace Acme\DemoBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use NGS\CoreBundle\DataFixtures\AbstractDataFixture;
use NGS\UserBundle\Entity\User;
class LoadDevUsersData extends AbstractDataFixture
{
/**
* Performs the actual fixtures loading.
*
* @see \Doctrine\Common\DataFixtures\FixtureInterface::load()
*
* @param ObjectManager $manager The object manager.
*/
protected function doLoad(ObjectManager $manager)
{
$userManager = $this->container->get('fos_user.user_manager');
// Create a new user
/** @var User $user */
$user = $userManager->createUser();
$user->setUsername('demo');
$user->setEmail('demo@local');
$user->setPlainPassword('demo');
$user->setEnabled(true);
$user->setFirstname('The');
$user->setLastname('Name');
$user->setAddress('10 rue de la démo');
$user->setCity('17000');
$user->setZipcode('La Rochelle');
$user->setCountry('France');
$user->setRoles(
array(
'ROLE_USER'
)
);
$this->addReference('user-demo', $user);
$manager->persist($user);
// Create a new user
/** @var User $user */
$user = $userManager->createUser();
$user->setFirstname('The');
$user->setLastname('Name');
$user->setUsername('admin');
$user->setEmail('admin@local');
$user->setPlainPassword('admin');
$user->setEnabled(true);
$user->setAddress('10 rue des admins');
$user->setCity('17340');
$user->setZipcode('Chatelaillon');
$user->setCountry('France');
$user->setRoles(
array(
'ROLE_ADMIN'
)
);
$this->addReference('user-admin', $user);
$manager->persist($user);
$manager->flush();
}
/**
* Returns the environments the fixtures may be loaded in.
*
* @return array The name of the environments.
*/
protected function getEnvironments()
{
return array('dev');
}
/**
* Get the order of this fixture
*
* @return integer
*/
public function getOrder()
{
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment