Created
June 9, 2011 23:58
-
-
Save j/1018025 to your computer and use it in GitHub Desktop.
User Entity
This file contains 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 JStout\MainBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM, | |
Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\Entity(repositoryClass="JStout\MainBundle\Entity\AdvertiserRepository") | |
* @ORM\Table(name="advertiser") | |
*/ | |
class Advertiser | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="IDENTITY") | |
*/ | |
protected $id; | |
/** | |
* @var \JStout\MainBundle\Entity\User | |
* | |
* @ORM\ManyToMany(targetEntity="User", inversedBy="advertisers", cascade={"persist"}) | |
*/ | |
protected $users; | |
/** | |
* @var \JStout\MainBundle\Entity\Offer | |
* | |
* @ORM\OneToMany(targetEntity="Offer", mappedBy="advertiser", cascade={"persist"}) | |
*/ | |
protected $offers; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string") | |
* @Assert\NotBlank(message="The advertiser name cannot be blank!") | |
* @Assert\MaxLength(limit="255", message="The advertiser name cannot exceed 255 characters") | |
*/ | |
protected $name; | |
/** | |
* @var string | |
* | |
* @ORM\Column(type="string", nullable="true") | |
* @Assert\MaxLength(limit="255", message="The advertiser description cannot exceed 255 characters") | |
*/ | |
protected $description; | |
public function __construct() | |
{ | |
$this->users = new \Doctrine\Common\Collections\ArrayCollection(); | |
$this->offers = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer $id | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set name | |
* | |
* @param string $name | |
*/ | |
public function setName($name) | |
{ | |
$this->name = $name; | |
} | |
/** | |
* Get name | |
* | |
* @return string $name | |
*/ | |
public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* Set description | |
* | |
* @param string $description | |
*/ | |
public function setDescription($description) | |
{ | |
$this->description = $description; | |
} | |
/** | |
* Get description | |
* | |
* @return string $description | |
*/ | |
public function getDescription() | |
{ | |
return $this->description; | |
} | |
/** | |
* Add user | |
* | |
* @param JStout\MainBundle\Entity\User $users | |
*/ | |
public function addUser(\JStout\MainBundle\Entity\User $users) | |
{ | |
$this->users[] = $users; | |
} | |
/** | |
* Get users | |
* | |
* @return Doctrine\Common\Collections\Collection $users | |
*/ | |
public function getUsers() | |
{ | |
return $this->users; | |
} | |
/** | |
* Add offers | |
* | |
* @param JStout\MainBundle\Entity\Offer $offers | |
*/ | |
public function addOffers(\JStout\MainBundle\Entity\Offer $offers) | |
{ | |
$this->offers[] = $offers; | |
} | |
/** | |
* Get offers | |
* | |
* @return Doctrine\Common\Collections\Collection $offers | |
*/ | |
public function getOffers() | |
{ | |
return $this->offers; | |
} | |
public function __toString() | |
{ | |
return $this->name; | |
} | |
/** | |
* Add users | |
* | |
* @param JStout\MainBundle\Entity\User $users | |
*/ | |
public function addUsers(\JStout\MainBundle\Entity\User $users) | |
{ | |
$this->users[] = $users; | |
} | |
} |
This file contains 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 | |
[...] | |
/** | |
* @var \JStout\MainBundle\Entity\Advertiser | |
* | |
* @ORM\ManyToMany(targetEntity="Advertiser", mappedBy="users", cascade={"all"}) | |
*/ | |
protected $advertisers; | |
[...] | |
// setter needed for Symfony2 Forms (form creates a multi-select and calls this method) | |
public function setAdvertisers(\Doctrine\Common\Collections\ArrayCollection $advertisers) | |
{ | |
$this->advertisers = $advertisers; | |
} | |
[...] |
This file contains 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 | |
[...] | |
/** | |
* @Extra\Route(name="user_index") | |
* @Extra\Template() | |
*/ | |
public function indexAction($user = null) | |
{ | |
if (!$user instanceOf User) { | |
$new = true; | |
$user = new User(); | |
} else { | |
$new = false; | |
} | |
$form = $this->get('form.factory')->create(new UserType($new), $user); | |
$formHandler = $this->get('form.handler')->create(new UserHandler(), $form); | |
// process form | |
if ($formHandler->process($this->container->get('security.encoder_factory')->getEncoder($user), $new)) { | |
$this->get('session')->setFlash('notice', 'Successfully ' . ($new ? 'added' : 'edited') . ' user!'); | |
return $this->redirect($this->generateUrl('user_index')); | |
} | |
[...] |
This file contains 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 | |
[...] | |
public function process($encoder, $new = true) | |
{ | |
if ('POST' == $this->request->getMethod()) { | |
// bind form data | |
$this->form->bindRequest($this->request); | |
// If form is valid | |
if ($this->form->isValid() && ($user = $this->form->getData()) instanceof User) { | |
// if user is new or password isn't null | |
if (true === $new) { | |
$user->setSalt(base_convert(sha1(uniqid(mt_rand(), true)), 16, 36)); | |
$user->setPassword($encoder->encodePassword($user->getPlainPassword(), $user->getSalt())); | |
} | |
// save user to the database | |
$this->entityManager->persist($user); | |
$this->entityManager->flush(); | |
return true; | |
} | |
} | |
[...] |
This file contains 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 | |
[...] | |
class UserType extends AbstractType | |
{ | |
public $new; | |
public function __construct($new = true) | |
{ | |
$this->new = $new; | |
} | |
public function buildForm(FormBuilder $builder, array $options) | |
{ | |
$builder | |
->add('advertisers') | |
->add('publisherNumber') | |
->add('firstName') | |
->add('lastName') | |
->add('username') | |
->add('plainPassword', 'repeated', array('type' => 'password')); | |
} | |
public function getDefaultOptions(array $options) | |
{ | |
return array( | |
'data_class' => 'JStout\MainBundle\Entity\User', | |
'validation_groups' => $this->new ? 'Registration' : 'Edit' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment