Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppEntity;
- use DoctrineORMMapping as ORM;
- use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
- use SymfonyComponentSecurityCoreUserUserInterface;
- /**
- * User
- *
- * @ORMEntity
- * @UniqueEntity(fields="email", message="Email already taken")
- * @ORMHasLifecycleCallbacks()
- */
- class User implements UserInterface
- {
- use MappingUserTrait;
- /**
- * @param Person $person
- */
- public function addPeople(Person $person)
- {
- $this->people->add($person);
- $person->setOwner($this);
- }
- /**
- * @param Person $person
- */
- public function removePeople(Person $person)
- {
- $this->people->removeElement($person);
- }
- }
- <?php
- namespace AppEntityMapping;
- use AppEntityPerson;
- use DoctrineCommonCollectionsArrayCollection;
- use DoctrineORMMapping as ORM;
- use SymfonyComponentValidatorConstraints as Assert;
- /**
- * User
- *
- * @ORMTable()
- */
- trait UserTrait
- {
- /**
- * @var int
- *
- * @ORMColumn(name="id", type="integer")
- * @ORMId
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var string
- *
- * @ORMColumn(name="hash", type="string")
- */
- private $hash;
- /**
- * @var bool
- *
- * @ORMColumn(name="is_active", type="boolean")
- */
- private $isActive;
- /**
- * @var DateTime
- *
- * @ORMColumn(name="updated_at", type="datetime")
- */
- private $updatedAt;
- /**
- * @var DateTime
- *
- * @ORMColumn(name="created_at", type="datetime")
- */
- private $createdAt;
- /**
- * @var string
- * @ORMColumn(name="email", type="string")
- * @AssertNotBlank
- * @AssertEmail
- */
- private $email;
- /**
- * @var DoctrineCommonCollectionsCollection
- *
- * @ORMOneToMany(targetEntity="AppEntityPerson", mappedBy="owner", cascade={"persist"})
- */
- private $people;
- /**
- * @var string
- * @AssertNotBlank
- * @AssertLength(max=4096)
- */
- private $plainPassword;
- /**
- * Constructor
- */
- public function __construct()
- {
- $this->people = new ArrayCollection();
- $this->roles = ['ROLE_USER'];
- $this->isActive = 1;
- }
- /**
- * @return int
- */
- public function getId(): int
- {
- return $this->id;
- }
- /**
- * @param int $id
- */
- public function setId(int $id): void
- {
- $this->id = $id;
- }
- /**
- * @return bool
- */
- public function isActive(): bool
- {
- return $this->isActive;
- }
- /**
- * @param bool $isActive
- */
- public function setIsActive(bool $isActive): void
- {
- $this->isActive = $isActive;
- }
- /**
- * @return string
- */
- public function getEmail()
- {
- return $this->email;
- }
- /**
- * @param string $email
- */
- public function setEmail(string $email): void
- {
- $this->email = $email;
- }
- /**
- * @return DoctrineCommonCollectionsCollection
- */
- public function getPeople(): DoctrineCommonCollectionsCollection
- {
- return $this->people;
- }
- /**
- * @param DoctrineCommonCollectionsCollection $people
- */
- public function setPeople(DoctrineCommonCollectionsCollection $people): void
- {
- $this->people = $people;
- }
- /**
- * @return string
- */
- public function getPlainPassword()
- {
- return $this->plainPassword;
- }
- /**
- * @param string $plainPassword
- */
- public function setPlainPassword(string $plainPassword): void
- {
- $this->plainPassword = $plainPassword;
- }
- /**
- * Implementation of UserInterface: Get password hash.
- * @return string
- */
- public function getPassword()
- {
- return $this->hash;
- }
- /**
- * Implementation of UserInterface
- */
- public function eraseCredentials()
- {
- $this->plainPassword = null;
- }
- /**
- * Implementation of UserInterface
- */
- public function getUsername()
- {
- return $this->email;
- }
- public function getSalt()
- {
- return null;
- }
- public function getRoles()
- {
- return ["User"];
- }
- /**
- * @return string
- */
- public function getHash(): string
- {
- return $this->hash;
- }
- /**
- * @param string $hash
- */
- public function setHash(string $hash): void
- {
- $this->hash = $hash;
- }
- /**
- * @ORMPrePersist
- * @ORMPreUpdate
- */
- public function updatedTimestamps(): void
- {
- $dateTimeNow = new DateTime('now');
- $this->setUpdatedAt($dateTimeNow);
- if ($this->getCreatedAt() === null) {
- $this->setCreatedAt($dateTimeNow);
- }
- }
- /**
- * @return DateTime
- */
- public function getUpdatedAt(): DateTime
- {
- return $this->updatedAt;
- }
- /**
- * @param DateTime $updatedAt
- */
- public function setUpdatedAt(DateTime $updatedAt): void
- {
- $this->updatedAt = $updatedAt;
- }
- /**
- * @return DateTime
- */
- public function getCreatedAt()
- {
- return $this->createdAt;
- }
- /**
- * @param DateTime $createdAt
- */
- public function setCreatedAt(DateTime $createdAt): void
- {
- $this->createdAt = $createdAt;
- }
- }
- <?php
- namespace AppEntity;
- use DoctrineORMMapping as ORM;
- /**
- * Person
- *
- * @ORMEntity
- */
- class Person
- {
- use MappingPersonTrait;
- }
- <?php
- namespace AppEntityMapping;
- use DoctrineORMMapping as ORM;
- use SymfonyComponentValidatorConstraints as Assert;
- /**
- * Person
- *
- * @ORMTable()
- */
- trait PersonTrait
- {
- /**
- * @var int
- *
- * @ORMColumn(name="id", type="integer")
- * @ORMId
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- * @var string
- *
- * @ORMColumn(name="family_name", type="string")
- * @AssertNotBlank
- */
- private $familyName;
- /**
- * @var string
- *
- * @ORMColumn(name="given_name", type="string")
- * @AssertNotBlank
- */
- private $givenName;
- /**
- * @var string
- *
- * @ORMColumn(name="title", type="string")
- */
- private $title;
- /**
- * @var DateTime
- *
- * @ORMColumn(name="birth_date", type="datetime")
- */
- private $birthDate;
- /**
- * @var string
- *
- * @ORMColumn(name="salutation", type="string")
- */
- private $salutation;
- /**
- * @var string
- *
- * @ORMColumn(name="gender", type="string")
- */
- private $gender;
- /**
- * @var AppEntityUser
- *
- * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="people")
- * @ORMJoinColumns({
- * @ORMJoinColumn(name="owner_id", referencedColumnName="id")
- * })
- */
- private $owner;
- /**
- * @return string
- */
- public function getFamilyName()
- {
- return $this->familyName;
- }
- /**
- * @param string $familyName
- */
- public function setFamilyName($familyName)
- {
- var_dump($familyName);
- $this->familyName = $familyName;
- }
- /**
- * @return string
- */
- public function getGivenName()
- {
- return $this->givenName;
- }
- /**
- * @param string $givenName
- */
- public function setGivenName($givenName)
- {
- $this->givenName = $givenName;
- }
- /**
- * @return string
- */
- public function getTitle(): string
- {
- return $this->title;
- }
- /**
- * @param string $title
- */
- public function setTitle(string $title): void
- {
- $this->title = $title;
- }
- /**
- * @return DateTime
- */
- public function getBirthDate(): DateTime
- {
- return $this->birthDate;
- }
- /**
- * @param DateTime $birthDate
- */
- public function setBirthDate(DateTime $birthDate): void
- {
- $this->birthDate = $birthDate;
- }
- /**
- * @return string
- */
- public function getSalutation(): string
- {
- return $this->salutation;
- }
- /**
- * @param string $salutation
- */
- public function setSalutation(string $salutation): void
- {
- $this->salutation = $salutation;
- }
- /**
- * @return string
- */
- public function getGender(): string
- {
- return $this->gender;
- }
- /**
- * @param string $gender
- */
- public function setGender(string $gender): void
- {
- $this->gender = $gender;
- }
- /**
- * @return AppEntityUser
- */
- public function getOwner(): AppEntityUser
- {
- return $this->owner;
- }
- /**
- * @param AppEntityUser $owner
- */
- public function setOwner(AppEntityUser $owner): void
- {
- $this->owner = $owner;
- }
- }
- <?php
- namespace AppForm;
- use AppEntityUser;
- use SymfonyComponentFormAbstractType;
- use SymfonyComponentFormExtensionCoreTypeCollectionType;
- use SymfonyComponentFormExtensionCoreTypeEmailType;
- use SymfonyComponentFormExtensionCoreTypePasswordType;
- use SymfonyComponentFormExtensionCoreTypeRepeatedType;
- use SymfonyComponentFormFormBuilderInterface;
- use SymfonyComponentOptionsResolverOptionsResolver;
- class UserType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('email', EmailType::class)
- ->add('plainPassword', PasswordType::class)
- ->add(
- "people",
- CollectionType::class,
- [
- 'entry_type' => PersonType::class,
- 'allow_add' => true,
- 'by_reference' => false,
- ]
- );
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults(
- [
- 'data_class' => User::class,
- 'csrf_protection' => false
- ]
- );
- }
- }
- <?php
- namespace AppForm;
- use AppEntityPerson;
- use SymfonyComponentFormAbstractType;
- use SymfonyComponentFormExtensionCoreTypeTextType;
- use SymfonyComponentFormFormBuilderInterface;
- use SymfonyComponentOptionsResolverOptionsResolver;
- class PersonType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('familyName', TextType::class)
- ->add('givenName', TextType::class);
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults(
- [
- 'data_class' => Person::class,
- 'csrf_protection' => false,
- ]
- );
- }
- }
- /**
- * @Route("/register")
- * @param Request $request
- * @param UserPasswordEncoderInterface $passwordEncoder
- */
- public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
- {
- $user = new User();
- $form = $this->createForm(UserType::class, $user);
- $form->submit($request->request->all());
- if ($form->isSubmitted() && $form->isValid()) {
- $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
- $user->setHash($password);
- $entityManager = $this->getDoctrine()->getManager();
- $entityManager->persist($user);
- $entityManager->flush();
- return new Response("ok", 300);
- }
- return new Response("not ok", 500);
- }
Add Comment
Please, Sign In to add comment