Symfony 2.3.7来自查询结果的表单值

时间:2013-12-11 11:25:59

标签: php mysql sql forms symfony

我从我的实体创建了一个表单(使用Symfony 2.3.7),我有一个问题: 我想把查询结果放在我的表单值。 前名:john(名字在我的表中)。 我的表格类是:

class form extends AbstractType{
   private $repository;
    private $idRistorant;

    public function __construct(EntityRepository $repository,$idRist)

    {
        $this->repository = $repository;
        $this->idRistorant = $idRist;
    }

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('dataInser')
            ->add('username')
            ->add('password')
}

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'sgnamPanelBundle\sgnamPanelBundle\Entity\OdlineRistoranti'

        ));
    }


    public function getName()
    {
        return 'horizontal';
    }
}

2 个答案:

答案 0 :(得分:0)

除非我不理解您的问题,但默认情况下,您在控制器中创建表单(我希望您已完成的工作)与您在数据库中获得的实体一致。 Symfony2将使用实体的值填充表单输入。

答案 1 :(得分:0)

这是一个例子 在我的控制器中:

// I want the getData() Result into value Form (<input type="text" value="username of //query Result">)

public function getData(){
     $repository = $this->getDoctrine()
        ->getRepository('panelAdiminResturantBundle:OdlineRistoranti');
        $user = $repository->findBy(array('username' => $username,
            'passwordWeb'=>$password));
    }

    public function editResturantAction (Request $request){
        $form = $this->createForm(new OdlineRistorantiType());
        return $this->render('panelAdiminResturantBundle:Default:editResturant.html.twig',
                               array('form'=>$form->createView()));
        }   

我的表单类

class form extends AbstractType{
   private $repository;
    private $idRistorant;

    public function __construct(EntityRepository $repository,$idRist)

    {
        $this->repository = $repository;
        $this->idRistorant = $idRist;
    }



    public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('dataInser')
                ->add('username')
                ->add('password')
    }

        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'sgnamPanelBundle\sgnamPanelBundle\Entity\OdlineRistoranti'

            ));
        }

我的实体:

use Doctrine\ORM\Mapping as ORM;

/**
 * OdlineRistoranti
 *
 * @ORM\Table(name="odline_ristoranti")
 * @ORM\Entity
 */
class OdlineRistoranti
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="data_inser", type="date", nullable=false)
     */
    private $dataInser;

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

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

    /**
     * @var string
     *
     * @ORM\Column(name="password_web", type="string", length=255, nullable=false)
     */
    private $passwordWeb;
.
.
.
.
}