How to create a form with multiple rows of one entity in Symfony2

时光总嘲笑我的痴心妄想 提交于 2019-11-28 12:20:40

Here's a start on a possible solution. The example below takes a single entity Skills and presents all of them on a single page. What I don't know is whether this technique can be used to persist children objects. I would expect one could loop through the returned data and persist as required.

The code below results in a page with a list of all possible Skills and a checkbox for declaring each enabled or enabled.

In a controller:

    $skills = $em->getRepository("TruckeeMatchingBundle:Skill")->getSkills();
    $formSkills = $this->createForm(new SkillsType(), array('skills' => $skills));
    ...
        if ($request->getMethod() == 'POST') {
            $formSkills->handleRequest($request);
            foreach ($skills as $existingSkill) {
                $em->persist($existingSkill);
            }
        }
    ...
    return ['formSkills' => $formSkills->createView(),...]

In a template:

{% for skill in formSkills.skills %}
    {{ skill.vars.value.skill }}
            <input type="hidden" name="skills[skills][{{ loop.index0 }}][skill]" value="{{ skill.vars.value.skill }}">
            <input type="checkbox" name="skills[skills][{{ loop.index0 }}][enabled]" 
            {%if skill.vars.value.enabled %}checked="checked"{%endif%}
{% endfor %}

I use a different strategy. My TWIG file is similar to that of Monica's question. But has a few but very useful differences. Here your code:

     {{ form_start(form) }}  
  {% for docente in docentes %}  
      Id: <input type="integer"  name="{{ docente.id }}" required="required"   style="width:30px" value="{{ docente.id }}" readonly> 
      Apellido: <input type="text"  name="{{ docente.apellido }}" required="required"  style="width: 80px" value="{{ docente.apellido }}" readonly>
       Nombres: <input type="text"  name="{{ docente.nombres }}" required="required"  style="width: 80px" value="{{ docente.nombres }}" readonly>
     Discrecional:  <input type="checkbox"  name="D{{ docente.id }}"  value="{{ docente.discrecional }}"  {% if docente.discrecional==1 %}checked{% endif %}> <br> 
      <br>
  {% endfor %}        
       <input type="submit" value="Grabar" />      
 {{ form_end(form) }} 

In TWIG file, I proceed to create a different name for each record in the form for the "discrecional" field. This will help me to identify each record in the controller. Watch up.

Once the user presses the "submit" button I perform an iteration in my Controller file, as shown below:

if ($form->isSubmitted() && $form->isValid()) {     
            $i=0;
                foreach ($defaultData as $value) {
                     $data2= array('id' =>$request->request->get($defaultData[$i]['id']), 
                  'discrecional' =>$request->request->get('D'.$defaultData[$i]['id'])); 

                    if (($request->request->get('D'.$defaultData[$i]['id'])== '0' and $defaultData[$i]['discrecional']=='0') or
                        ($request->request->get('D'.$defaultData[$i]['id'])== NULL and $defaultData[$i]['discrecional']=='1'))    
                    {
                     $em->getRepository('BackendBundle:Docentes')->findDocenteFiltId2($data2);
                    }
                    $i=$i+1; 
                }

But the update of registers, is a work that is done in my Repository file through a query using UPDATE, instead of doing it in the Controller file. To avoid unnecessary queries and a server overload, I only do an UPDATE of records that have previously changed. In the example, the following lines in my controller check if there has been a change in the record (in my case, I'm just editing a field called "discrecional". If the field has changed, then I call the query and update the record):

if (($request->request->get('D'.$defaultData[$i]['id'])== '0' and $Data[$i]['discrecional']=='0') or
                    ($request->request->get('D'.$defaultData[$i]['id'])== NULL and $defaultData[$i]['discrecional']=='1'))    
                {
                 $em->getRepository('BackendBundle:Docentes')->findDocenteFiltId2($data2);
                }

My complete Controller file is here:

public function discrecionalAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();     
    $defaultData= $em->getRepository('BackendBundle:Docentes')->buscarDocentesActivos2();


// construimos un  formulario "vacío" sin campos definido
            $form = $this->createFormBuilder($defaultData);
        $form = $form->getForm();

            $form->handleRequest($request);    


            if ($form->isSubmitted() && $form->isValid()) {


            $i=0;
                foreach ($defaultData as $value) {
                     $data2= array('id' =>$request->request->get($defaultData[$i]['id']), 
                  'discrecional' =>$request->request->get('D'.$defaultData[$i]['id'])); 

                    if (($request->request->get('D'.$defaultData[$i]['id'])== '0' and $defaultData[$i]['discrecional']=='0') or
                        ($request->request->get('D'.$defaultData[$i]['id'])== NULL and $defaultData[$i]['discrecional']=='1'))    
                    {
                     $em->getRepository('BackendBundle:Docentes')->findDocenteFiltId2($data2);
                    }
                    $i=$i+1; 
                }

    return $this->redirectToRoute('docentes_discrecional');

               }        


      return $this->render('docentes/discrecional.html.twig', array(
                 'docentes' =>$defaultData,
        'form' => $form->createView() ));
}

My complete first Repository query is here:

public function buscarDocentesActivos2()
{


        $fields = array('d.id', 'd.apellido', 'd.nombres', 'd.discrecional');
$query = $this->getEntityManager()->createQueryBuilder();
        $query
            ->select($fields)
            ->from('BackendBundle:Docentes', 'd')
           ->where('d.activo=true')
           ->orderBy('d.apellido, d.nombres');     

        $consulta = $query->getQuery()->getResult();

        return $consulta;
    }

My complete final Repository query with the UPDATE function is here:

public function findDocenteFiltId2($filtro)
{

 if (is_null($filtro['discrecional'])){ 
     $discrec= '0';
 };

 if ($filtro['discrecional']=='0'){ 
     $discrec= '1';
 };

 $em = $this->getEntityManager();

    $consulta = $em->createQuery('
        UPDATE BackendBundle:Docentes d
        SET d.discrecional = :disc
        WHERE d.id = :idver
    ');

   $consulta->setParameters(array(

    'idver' => $filtro['id'],
    'disc'  => $discrec,     
        ));
      return $consulta->getArrayResult();

}   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!