修改FOSUser Bundle

时间:2016-04-28 17:11:09

标签: php symfony fosuserbundle symfony-forms

我尝试创建它,以便管理员可以在不使用命令行的情况下创建/编辑/删除用户。我有一个用户实体和许多用户来测试。在创建和编辑的形式中,没有任何东西在那里。我查看了文档,让我感到困惑。我试图遵循它,但我无法理解container不是一个对象的原因。我不确定它来自哪些文档。

The Documentation

用户控制器:

namespace Pa\ShopTestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Pa\ShopTestBundle\Entity\User;
use Pa\ShopTestBundle\Form\UserType;
use FOS\UserBundle\Doctrine\UserManager;

/**
 * User controller.
 *
 * @Route("/user")
 */
class UserController extends Controller
{

    /**
     * Lists all User entities.
     *
     * @Route("/", name="user")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entities = $em->getRepository('PaShopTestBundle:User')->findAll();

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

        return array(
            'entities' => $entities,
        );
    }

    /**
     * Creates a new User entity.
     *
     * @Route("/", name="user_create")
     * @Method("POST")
     * @Template("PaShopTestBundle:User:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new User();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Creates a form to create a User entity.
     *
     * @param User $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new User entity.
     *
     * @Route("/new", name="user_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
     //create a new user DOES NOTHING ------------------------------------------------------------------------------------------------------FIX
        $entity = new User();
        $form   = $this->createCreateForm($entity);

        // $UserManager = $container->get('fos_user.user_manager');
        // $user = $UserManager->createUser();

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a User entity.
     *
     * @Route("/{id}", name="user_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('PaShopTestBundle:User')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Displays a form to edit an existing User entity.
     *
     * @Route("/{id}/edit", name="user_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {

        //EDIT DOES NOTHING ------------------------------------------------------------------------------------------------------FIX
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('PaShopTestBundle:User')->find($id);

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        $editForm = $this->createEditForm($entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
    * Creates a form to edit a User entity.
    *
    * @param User $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(User $entity)
    {
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing User entity.
     *
     * @Route("/{id}", name="user_update")
     * @Method("PUT")
     * @Template("PaShopTestBundle:User:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('PaShopTestBundle:User')->find($id);

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find User entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
    /**
     * Deletes a User entity.
     *
     * @Route("/{id}", name="user_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('PaShopTestBundle:User')->find($id);

        if ($this->checkAdminLogin()) {
            throw $this->createNotFoundException('No Access.');
        }

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find User entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('user'));
    }

    /**
     * Creates a form to delete a User entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('user_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }

用户实体:

(我知道没有用户字段,但如果我真的需要它来做我想要的事情,我会感到困惑......)

namespace Pa\ShopTestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * User
 *
 * @ORM\Table(name="fos_user")
 * @ORM\Entity()
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="UserCart", mappedBy="user")
     */
    private $userCarts;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add userCarts
     *
     * @param \Pa\ShopTestBundle\Entity\UserCart $userCarts
     * @return User
     */
    public function addUserCart(\Pa\ShopTestBundle\Entity\UserCart $userCarts)
    {
        $this->userCarts[] = $userCarts;

        return $this;
    }

    /**
     * Remove userCarts
     *
     * @param \Pa\ShopTestBundle\Entity\UserCart $userCarts
     */
    public function removeUserCart(\Pa\ShopTestBundle\Entity\UserCart $userCarts)
    {
        $this->userCarts->removeElement($userCarts);
    }

    /**
     * Get userCarts
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUserCarts()
    {
        return $this->userCarts;
    }
}

0 个答案:

没有答案