Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataFixtures for SonataUserBundle Symfony2
<?php
namespace Application\Sonata\UserBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Application\Sonata\UserBundle\Entity\Group;
/**
* Creación inicial grupos Super-administrador y Administrador
*
* @author Juanjo García
*/
class LoadGroupData extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$groupSuperAdmin = new Group('Super-administrador', array("ROLE_SUPER_ADMIN"));
$manager->persist($groupSuperAdmin);
$manager->flush();
$this->addReference('super-admin-group', $groupSuperAdmin);
$groupAdmin = new Group('Administrador', array("ROLE_ADMIN"));
$manager->persist($groupAdmin);
$manager->flush();
}
public function getOrder()
{
// the order in which fixtures will be loaded
// the lower the number, the sooner that this fixture is loaded
return 1;
}
}
<?php
namespace Application\Sonata\UserBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Application\Sonata\UserBundle\Entity\User;
/**
* Creación inicial de usuario Super-administrador
*
* @author Juanjo García
*/
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function load(ObjectManager $manager)
{
$userAdmin = new User();
$userAdmin->setUsername('super-admin');
$userAdmin->setUsernameCanonical('super-admin');
$userAdmin->setEmail('your@email.com');
$userAdmin->setEmailCanonical('your@email.com');
$userAdmin->setFirstname('Juanjo');
$userAdmin->setLastname('García');
$userAdmin->setWebsite('http://www.editartgroup.com');
$userAdmin->setGender('m');
$userAdmin->setLocale('es_ES');
$userAdmin->setTimezone('Europe/Madrid');
$userAdmin->setRealRoles(array("ROLE_SUPER_ADMIN"));
$userAdmin->setEnabled(1);
$userAdmin->setGroups(array($this->getReference('super-admin-group')));
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($userAdmin, 'your_password');
$userAdmin->setPassword($password);
$manager->persist($userAdmin);
$manager->flush();
$this->addReference('super-admin-user', $userAdmin);
}
public function getOrder()
{
// the order in which fixtures will be loaded
// the lower the number, the sooner that this fixture is loaded
return 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment