Last active
May 5, 2016 01:44
-
-
Save xmon/2dbec6842560eb1378f2523703bc8812 to your computer and use it in GitHub Desktop.
DataFixtures for SonataUserBundle Symfony2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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('[email protected]'); | |
$userAdmin->setEmailCanonical('[email protected]'); | |
$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