Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在Symfony 2中以我的形式写入我的实体数据_Forms_Symfony_Doctrine Orm - Fatal编程技术网

Forms 如何在Symfony 2中以我的形式写入我的实体数据

Forms 如何在Symfony 2中以我的形式写入我的实体数据,forms,symfony,doctrine-orm,Forms,Symfony,Doctrine Orm,我正在Symfony 2中处理一个表单,我想知道是否有办法在表单字段中写入实体的数据,但我想在表单类中这样做 我知道我可以将这些数据从我的控制器传递到我的表单,但是因为我的表单知道它映射到哪个实体,所以我认为可以在表单中获取这些信息并将其放在我的字段中。我的表单映射到我的实体“infos”。它将被视为无连接,并且在该表中始终只有一个条目,因此只需要获取第一个条目(如果至少有一个条目) 我按照德斯托菲尔的建议做了。我将实体传递到我的表单,如下所示: <?php namespace Admi

我正在Symfony 2中处理一个表单,我想知道是否有办法在表单字段中写入实体的数据,但我想在表单类中这样做

我知道我可以将这些数据从我的控制器传递到我的表单,但是因为我的表单知道它映射到哪个实体,所以我认为可以在表单中获取这些信息并将其放在我的字段中。我的表单映射到我的实体“infos”。它将被视为无连接,并且在该表中始终只有一个条目,因此只需要获取第一个条目(如果至少有一个条目)


我按照德斯托菲尔的建议做了。我将实体传递到我的表单,如下所示:

<?php

namespace AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AdminBundle\Form\ModifierInfosType;

class InfoController extends Controller
{
  public function indexAction(Request $request)
  {
    //Tente de trouver l'enregistrement de la table info
    $infos = $this->getDoctrine()->getRepository('PublicBundle:Infos')->find(1);

    //Formulaire pour modifier les infos
    $form = $this->createForm(new ModifierInfosType(), $infos);

    //On surveille le formulaire
    $form->handleRequest($request);

    return $this->render('AdminBundle::info.html.twig', array(
         'form'=> $form->createView()
    ));
  }
}

将其传递到控制器中,无需其他任何操作。这是一个很好的实践。当你说在我的控制器中传递它时,你的意思是将它传递到我的窗体还是我的小枝,因为我可以这样做。我假设,你在控制器中调用createForm方法。如果将基础对象传递给createForm方法,则该对象的数据将在表单中,因此将在视图中预设。是!它会自动写入数据,非常感谢!
<?php

namespace PublicBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Projet Inter
 *
 * @ORM\Table(name="pt_infos");
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="PublicBundle\Entity\InfosDepot")
 */
class Infos
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    //ID du projet
    protected $id;

    /**
     * @ORM\Column(name="inf_travail_fr", type="text",length=100)
     */
    //Poste occupé
    protected $travailFr;

    /**
     * @ORM\Column(name="inf_travail_en", type="text",length=100)
     */
    //Poste occupé
    protected $travailEn;

    /**
     * @ORM\Column(name="inf_lien", type="text",length=100)
     */
    //Lien vers l'emploie
    protected $lien;

    /**
     * @ORM\Column(name="inf_linkedin", type="text",length=100)
     */
    //Lien vers la page linkedin
    protected $linkedin;


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

    /**
     * Set travailFr
     *
     * @param string $travailFr
     * @return Infos
     */
    public function setTravailFr($travailFr)
    {
        $this->travailFr = $travailFr;

        return $this;
    }

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

    /**
     * Set travailEn
     *
     * @param string $travailEn
     * @return Infos
     */
    public function setTravailEn($travailEn)
    {
        $this->travailEn = $travailEn;

        return $this;
    }

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

    /**
     * Set lien
     *
     * @param string $lien
     * @return Infos
     */
    public function setLien($lien)
    {
        $this->lien = $lien;

        return $this;
    }

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

    /**
     * Set linkedin
     *
     * @param string $linkedin
     * @return Infos
     */
    public function setLinkedin($linkedin)
    {
        $this->linkedin = $linkedin;

        return $this;
    }

    /**
     * Get linkedin
     *
     * @return string 
     */
    public function getLinkedin()
    {
        return $this->linkedin;
    }
}
<?php

namespace AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AdminBundle\Form\ModifierInfosType;

class InfoController extends Controller
{
  public function indexAction(Request $request)
  {
    //Tente de trouver l'enregistrement de la table info
    $infos = $this->getDoctrine()->getRepository('PublicBundle:Infos')->find(1);

    //Formulaire pour modifier les infos
    $form = $this->createForm(new ModifierInfosType(), $infos);

    //On surveille le formulaire
    $form->handleRequest($request);

    return $this->render('AdminBundle::info.html.twig', array(
         'form'=> $form->createView()
    ));
  }
}