表单构建器,变量为默认值

时间:2017-08-08 23:50:14

标签: mysql fosuserbundle formbuilder symfony-2.6

我在Symfony2.8中使用表单构建器和FOS UserBundle设置默认变量时遇到问题

/**
   * @Route("/add/{id}", name="add")
   * @Template("add.html.twig")
   */
    public function createFormAction ($id, Request $request)
    {
      $user = $this->container->get('security.context')->getToken()->getUser();
      $username = $user->getUsername();
      $userid = $user->getId();

      $repo = $this->getDoctrine()->getRepository('AppBundle:Place');
      $pl = $repo->find($id);

      //var_dump($us);

      $form = $this -> createFormBuilder ($pl)
                       -> add( "id" , "integer" )
                       -> add( "placeuser", NULL)
                       -> add( "priceperhour", "hidden", array( "mapped" => false))
                       -> add( "rentdate" , "date" )
                       -> add( "rentlength" , "integer" )
                       ->add( 'save', 'submit', array('label' => 'Add'))
                       -> getForm ();

                       $form->handleRequest($request);
                       if ($form->isSubmitted())
                       {
                         $pl = $form->getData();
                         $em = $this->getDoctrine()->getManager();
                         $em->flush();
                         return $this->redirectToRoute('reservation');
                     }

          Return $this -> render ( 'add.html.twig' , [ 'form' => $form -> createView ()]);

    }

请看 - >在结果中添加(“placeuser”,NULL)我收到了包含表中所有用户名的下拉菜单。

我尝试更改为 - >添加(“placeuser”,“整数或文本”,数组(“data”=> $ username,或来自security.context的$ userid))但我收到错误“无法转换属性路径的值”placeuser“:预期一个数字/字符串。“只有当用户从下拉菜单中选择时,表格才有效。

实体地点:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Places
 *
 * @ORM\Entity
 * @ORM\Table(name="places")
 */

 class Place
 {
   /**
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="NONE")
    * 
    */
    private $id;

    /**
     * @ORM\Column(name="priceperhour", type="decimal", precision=4, scale=2, nullable=true)
     */
    private $priceperhour;

    /**
     * @ORM\Column(name="rentdate", type="date", nullable=true)
     */
    private $rentdate;

    /**
     * @ORM\Column(name="rentlength", type="decimal", nullable=true)
     */
    private $rentlength;

    /**

     * @ORM\ManyToOne(targetEntity="User", inversedBy="places")
     * @ORM\JoinColumn(name="placeuser", referencedColumnName="id")
     */
    private $placeuser;

    /**
     * Set id
     *
     * @param integer $id
     *
     * @return Place
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

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

    /**
     * Set priceperhour
     *
     * @param string $priceperhour
     *
     * @return Place
     */
    public function setPriceperhour($priceperhour)
    {
        $this->priceperhour = $priceperhour;

        return $this;
    }

    /**
     * Get priceperhour
     *
     * @return string
     */
    public function getPriceperhour()
    {
        return $this->priceperhour;
    }

    /**
     * Set rentdate
     *
     * @param string $rentdate
     *
     * @return Place
     */
    public function setRentdate($rentdate)
    {
        $this->rentdate = $rentdate;

        return $this;
    }

    /**
     * Get rentdate
     *
     * @return string
     */
    public function getRentdate()
    {
        return $this->rentdate;
    }

    /**
     * Set rentlength
     *
     * @param integer $rentlength
     *
     * @return Place
     */
    public function setRentlength($rentlength)
    {
        $this->rentlength = $rentlength;

        return $this;
    }

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

    /**
     * Set placeuser
     *
     * @param \AppBundle\Entity\User $placeuser
     *
     * @return Place
     */
    public function setPlaceuser(\AppBundle\Entity\User $placeuser = null)
    {
        $this->placeuser = $placeuser;

        return $this;
    }

    /**
     * Get placeuser
     *
     * @return \AppBundle\Entity\User
     */
    public function getPlaceuser()
    {
        return $this->placeuser;
    }
}

实体用户:

namespace AppBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    use FOS\UserBundle\Model\User as BaseUser;

    /**
     * User
     *
     * @ORM\Entity
     * @ORM\Table(name="users")
     * @UniqueEntity(fields="email", message="Email already taken")
     * @UniqueEntity(fields="username", message="Username already taken")
     */

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

        /**
         * @Assert\Length(min=5)
         */
        protected $username;

        /**
         * @Assert\Length(min=5)
         * @Assert\Length(max=4096)
         */
        protected $password;

        /**
         * @ORM\Column(name="name", type="string", length=20, nullable=true)
         */
        private $name;

        /**
         * @ORM\Column(name="surname", type="string", length=20, nullable=true)
         */
        private $surname;

        /**
         * @Assert\NotBlank()
         */
        protected $email;

        /**
         * @ORM\Column(name="accountvalue", type="decimal", precision=6, scale=2, nullable=true)
         */
        private $accountvalue;


        /**
         * @ORM\OneToMany (targetEntity="Place", mappedBy="placeuser")
         */
        private $places;

        public function __construct()
        {
          parent::__construct();
           $this->places = new ArrayCollection();
        }

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

        /**
         * Set name
         *
         * @param string $name
         *
         * @return User
         */
        public function setName($name)
        {
            $this->name = $name;

            return $this;
        }

        /**
         * Get name
         *
         * @return string
         */
        public function getName()
        {
            return $this->name;
        }

        /**
         * Set surname
         *
         * @param string $surname
         *
         * @return User
         */
        public function setSurname($surname)
        {
            $this->surname = $surname;

            return $this;
        }

        /**
         * Get surname
         *
         * @return string
         */
        public function getSurname()
        {
            return $this->surname;
        }

        /**
         * Set accountvalue
         *
         * @param string $accountvalue
         *
         * @return User
         */
        public function setAccountvalue($accountvalue)
        {
            $this->accountvalue = $accountvalue;

            return $this;
        }

        /**
         * Get accountvalue
         *
         * @return string
         */
        public function getAccountvalue()
        {
            return $this->accountvalue;
        }

        /**
         * Add place
         *
         * @param \AppBundle\Entity\Place $place
         *
         * @return User
         */
        public function addPlace(\AppBundle\Entity\Place $place)
        {
            $this->places[] = $place;

            return $this;
        }

        /**
         * Remove place
         *
         * @param \AppBundle\Entity\Place $place
         */
        public function removePlace(\AppBundle\Entity\Place $place)
        {
            $this->places->removeElement($place);
        }

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

我的目标是拥有当前登录用户的字段,并在此上下文中添加位置。

万分感谢您的帮助!

0 个答案:

没有答案