Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Admin extends BaseUser
- {
- /**
- * @ORM\Id()
- * @ORM\Column(name="idAdmin", type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * @var string
- * @ORM\Column(type="string", length=255, nullable=true, options={"default":NULL})
- */
- protected $style;
- /**
- * @ORM\ManyToMany(targetEntity="AdminBundle\Entity\AdminGroup")
- * @ORM\JoinTable(
- * name="admin_has_group",
- * joinColumns={
- * @ORM\JoinColumn(name="idAdmin", referencedColumnName="idAdmin")
- * },
- * inverseJoinColumns={
- * @ORM\JoinColumn(name="idGroup", referencedColumnName="idGroup")
- * }
- * )
- */
- protected $groups;
- /**
- * Admin constructor.
- */
- public function __construct()
- {
- parent::__construct();
- $this->groups = new ArrayCollection();
- }
- /**
- * @return string
- */
- public function getStyle()
- {
- return $this->style;
- }
- /**
- * @param string $style
- * @return Admin
- */
- public function setStyle($style)
- {
- $this->style = $style;
- return $this;
- }
- /**
- * @param ArrayCollection
- *
- * @return Admin
- */
- public function setGroups($groups)
- {
- $this->groups = $groups;
- return $this;
- }
- /**
- * @return ArrayCollection
- */
- public function getGroups()
- {
- return $this->groups;
- }
- /**
- * @param GroupInterface $group
- *
- * @return Admin
- */
- public function addGroup(GroupInterface $group)
- {
- if (empty($this->getGroups())) {
- $this->groups = new ArrayCollection();
- }
- if (!$this->getGroups()->contains($group)) {
- $this->getGroups()->add($group);
- }
- return $this;
- }
- /**
- * @param string $name
- *
- * @return bool
- */
- public function hasGroup($name="") {
- dump($name);
- if (!empty($name)) {
- return parent::hasGroup($name);
- }
- return false;
- }
- }
- --------------------------------------------------------------------------------
- class AdminGroup extends BaseGroup
- {
- /**
- * @var int
- * @ORM\Id
- * @ORM\Column(name="idGroup", type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- protected $id;
- /**
- * Group constructor.
- *
- * @param string $name
- * @param array $roles
- */
- public function __construct($name = '', $roles = array())
- {
- parent::__construct($name, $roles);
- }
- }
- --------------------------------------------------------------------------------
- public function newAction(Request $request)
- {
- $em = $this->getDoctrine()->getManager();
- $admin = new Admin();
- $form = $this->buildForm($admin, true);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- /** @var Admin $admin */
- $admin = $form->getData();
- $admin->setEmail($admin->getUsername());
- $admin->setEnabled(true);
- $em->persist($admin);
- $em->flush();
- return $this->redirectToRoute('backend_admin_list');
- }
- return $this->render(
- 'edit.html.twig', [
- 'form' => $form->createView(),
- ]);
- }
- public function editAction($id, Request $request)
- {
- $em = $this->getDoctrine()->getManager();
- $admin = $this->getDoctrine()->getRepository(Admin::class)->find($id);
- $form = $this->buildForm($admin);
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- /** @var Admin $admin */
- $admin = $form->getData();
- $em->persist($admin);
- $em->flush();
- return $this->redirectToRoute('index');
- }
- return $this->render(
- 'edit.html.twig', [
- 'form' => $form->createView(),
- ]);
- }
- protected function buildForm(Admin $admin)
- {
- return $this->createFormBuilder($admin)
- ->add("username", TextType::class, [
- 'required' => true,
- ])
- ->add('plainPassword', PasswordType::class, [
- 'label' => 'Passwort',
- 'required' => true,
- ])
- ->add(
- 'style', ChoiceType::class, [
- 'required' => false,
- 'choices' => [
- 'System' => '',
- 'AdminLTE' => 'adminlte',
- ],
- ])
- ->add('groups', EntityType::class, [
- 'class' => AdminGroup::class,
- 'multiple' => true,
- 'choice_label' => 'name',
- ])
- ->add('save', SubmitType::class, ['label' => 'Save'])
- ->getForm();
- }
Advertisement
Add Comment
Please, Sign In to add comment