/**
  * Generate representative username by name and set new username
  *
  * @param Representative $representative
  * @param int            $iteration
  *
  * @return string New username
  */
 public function generateRepresentativeUsername(Representative $representative, $iteration = 0)
 {
     // Generate canonical name
     $name = $representative->getFirstName() . $representative->getLastName();
     $name = preg_replace('/[^\\w]/i', '', $name);
     $name = strtolower($name);
     $name = $iteration ? $name . $iteration : $name;
     $representByUsername = $this->entityManager->getRepository('CivixCoreBundle:Representative')->findOneBy(array('username' => $name));
     if (is_null($representByUsername)) {
         $representative->setUsername($name);
     } else {
         $name = $this->generateRepresentativeUsername($representative, ++$iteration);
     }
     return $name;
 }
 public function load(ObjectManager $manager)
 {
     $representative = new Representative();
     $representative->setFirstName('Joseph');
     $representative->setLastName('Biden');
     $representative->setUsername('JosephBiden');
     $representative->setOfficialTitle('Vice President');
     $representative->setCity('Washington');
     $representative->setOfficialAddress('1600 Pennsylvania Avenue NW');
     $representative->setOfficialPhone('');
     $representative->setEmail('*****@*****.**');
     $representative->setDistrict($this->getReference('district_us'));
     $representative->setRepresentativeStorage($this->getReference('vice_president'));
     $this->addReference('vice_president_representative', $representative);
     $manager->persist($representative);
     $manager->flush($representative);
 }
 public function load(ObjectManager $manager)
 {
     // Create representative
     /** @var $representative Representative */
     $representative = new Representative();
     $representative->setFirstName(self::REPRESENTATIVE_NAME);
     $representative->setLastName(self::REPRESENTATIVE_NAME);
     $representative->setUsername(self::REPRESENTATIVE_NAME);
     $representative->setOfficialTitle(self::REPRESENTATIVE_TITLE);
     $representative->setOfficialAddress(self::REPRESENTATIVE_ADDRESS);
     $representative->setOfficialPhone(self::REPRESENTATIVE_PHONE);
     $representative->setEmail(self::REPRESENTATIVE_EMAIL);
     $representative->setCountry(self::REPRESENTATIVE_COUNTRY);
     $representative->setCity(self::REPRESENTATIVE_CITY);
     $representative->setDistrict($this->getReference('district_us'));
     //encode password according configuration
     $factory = $this->container->get('security.encoder_factory');
     $encoder = $factory->getEncoder($representative);
     $password = $encoder->encodePassword(self::REPRESENTATIVE_PASSWORD, $representative->getSalt());
     $representative->setPassword($password);
     $this->addReference('representative1', $representative);
     $manager->persist($representative);
     $manager->flush();
 }
 public function setUsername($username)
 {
     $this->__load();
     return parent::setUsername($username);
 }