2017-04-21 6 views
2

Ich habe ein Problem mit meinem Formular bearbeiten in meinem CRUD, ich benutze die Befehle zum Erstellen von Crud von Symfony 2.8, beim Einchecken in der Bearbeitungsansicht lädt es alle Felder des Datensatzes Ich suchte, aber die abhängigen Felder einer anderen Entität erscheinen leer (Field Cargo, Profesion, Rol, Departamento). Ich möchte wissen, wie man abhängige Felder mit ihren jeweiligen Informationen erscheinen lässt.Symfony 2 Formular bearbeiten mit leeren abhängigen Feldern

Das ist mein DatUsuarioType

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('username',TextType::class, array('attr'=>array('class'=>'form-control col-xs-10 col-sm-5', 'style' => 'margin-bottom:10px'),'label'=>'Usuario')) 
      ->add('password',PasswordType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
      ->add('nombre',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'))) 
      ->add('paterno',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Apellido Paterno')) 
      ->add('materno',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Apellido Materno')) 
      ->add('ci',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Carnet de Identidad')) 
      ->add('departamento',EntityType::class, array('class'=>'bdBundle:ClaDepartamento','label'=>'Departamento', 'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'data' => '$id', 'placeholder' => 'Escoge una Opcion',)) 
      ->add('fechaNac',DateType::class, array('widget'=>'single_text', 'html5' => false, 'input' => 'datetime','label'=>'Fecha de Nacimiento','format'=>'dd/MM/yyyy', 'attr'=> ['class'=>'form-control js-datepicker', 'style' => 'margin-bottom:10px','placeholder'=>'dd/mm/yyyy', 'readonly'=>true])) 
      ->add('telefono',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Telefono Fijo')) 
      ->add('celular',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Telefono Celular')) 
      ->add('email',EmailType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Correo Electronico')) 
      ->add('rol',EntityType::class, array('class'=>'bdBundle:DatRol','label'=>'Rol de Usuario', 'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'data' => '$id', 'placeholder' => 'Escoge una Opcion',)) 
      ->add('cargoUsuario',EntityType::class, array('class'=>'bdBundle:DatCargoUsuario','label'=>'Cargo de Usuario', 'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'data' => '$id', 'placeholder' => 'Escoge una Opcion',)) 
      ->add('profesion',EntityType::class, array('class'=>'bdBundle:ClaProfesion','label'=>'Profesion de Usuario', 'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'data' => '$id', 'placeholder' => 'Escoge una Opcion',)) 
      ->add('imagen',TextType::class, array('attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Foto de Perfil')) 
      ->add('estado',ChoiceType::class, array('choices'=>array(true=> 'Habilitado'),'attr'=>array('class'=>'form-control', 'style' => 'margin-bottom:10px'),'label'=>'Activo/Inactivo')); 


} 

Dieser DatUsuarioController mein Controller ist

public function editAction(Request $request, DatUsuario $datUsuario) 
{ 
    $deleteForm = $this->createDeleteForm($datUsuario); 
    $editForm = $this->createForm('gishay\bdBundle\Form\DatUsuarioType', $datUsuario); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $this->getDoctrine()->getManager()->flush(); 

     return $this->redirectToRoute('usuario_edit', array('id' => $datUsuario->getId())); 
    } 

    return $this->render('datusuario/edit.html.twig', array(
     'datUsuario' => $datUsuario, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

Dieser Teil meiner Ansicht edit.html.twig ist

<div class="panel-body"> 
          {{ form_start(edit_form) }} 
          {{ form_errors(edit_form) }} 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="username">Usuario</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.username, { 'attr': {'readonly': 'true'} }) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="password">Password</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.password, { 'attr': {'readonly': 'true'} }) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="nombre">Nombre</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.nombre) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="paterno">Apellido Paterno</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.paterno) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="materno">Apellido Materno</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.materno) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="ci">Carnet de Identidad</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.ci) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="departamento">Expedido</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.departamento) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="fechanac">Fecha de Nacimiento</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.fechaNac) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="telefono">Telefono</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.telefono) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="celular">Celular</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.celular) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="email">Email</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.email) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="rol">Rol</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.rol) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="cargo">Cargo de Usuario</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.cargoUsuario) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="profesion">Profesion</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.profesion) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="imagen">Imagen</label> 
            <div class="col-md-9"> 
             {{ form_widget(edit_form.imagen) }} 
            </div> 
           </div> 

           <div class="form-group"> 
            <label class="col-md-3 control-label" for="estado">Activo/Inactivo</label> 
            <div class="col-md-9"> 

             {{ form_widget(edit_form.estado, { 'attr': {'readonly': 'true'} }) }} 
            </div> 
           </div> 
          {{ form_end(edit_form) }} 

         </div> 

diese DatUsuario meine Entität .php

namespace gishay\bdBundle\Entity; 

Verwenden Sie Doctrine \ ORM \ Mapping als ORM;

/** * DatUsuario */ Klasse DatUsuario { /** * @var integer */ private $ id;

/** 
* @var string 
*/ 
private $username; 

/** 
* @var string 
*/ 
private $password; 

/** 
* @var string 
*/ 
private $nombre; 

/** 
* @var string 
*/ 
private $paterno; 

/** 
* @var string 
*/ 
private $materno; 

/** 
* @var string 
*/ 
private $ci; 

/** 
* @var \DateTime 
*/ 
private $fechaNac; 

/** 
* @var string 
*/ 
private $telefono; 

/** 
* @var string 
*/ 
private $celular; 

/** 
* @var string 
*/ 
private $email; 

/** 
* @var string 
*/ 
private $imagen; 

/** 
* @var boolean 
*/ 
private $estado; 

/** 
* @var \gishay\bdBundle\Entity\DatRol 
*/ 
private $rol; 

/** 
* @var \gishay\bdBundle\Entity\ClaDepartamento 
*/ 
private $departamento; 

/** 
* @var \gishay\bdBundle\Entity\DatCargoUsuario 
*/ 
private $cargoUsuario; 

/** 
* @var \gishay\bdBundle\Entity\ClaProfesion 
*/ 
private $profesion; 

public function __toString() 
{ 
    return $this->getUsername(); 
} 

public function __construct() 
{ 
    $this->DatUsuario = new ArrayCollection(); 
} 

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

/** 
* Set username 
* 
* @param string $username 
* @return DatUsuario 
*/ 
public function setUsername($username) 
{ 
    $this->username = $username; 

    return $this; 
} 

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

/** 
* Set password 
* 
* @param string $password 
* @return DatUsuario 
*/ 
public function setPassword($password) 
{ 
    $this->password = $password; 

    return $this; 
} 

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

/** 
* Set nombre 
* 
* @param string $nombre 
* @return DatUsuario 
*/ 
public function setNombre($nombre) 
{ 
    $this->nombre = $nombre; 

    return $this; 
} 

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

/** 
* Set paterno 
* 
* @param string $paterno 
* @return DatUsuario 
*/ 
public function setPaterno($paterno) 
{ 
    $this->paterno = $paterno; 

    return $this; 
} 

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

/** 
* Set materno 
* 
* @param string $materno 
* @return DatUsuario 
*/ 
public function setMaterno($materno) 
{ 
    $this->materno = $materno; 

    return $this; 
} 

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

/** 
* Set ci 
* 
* @param string $ci 
* @return DatUsuario 
*/ 
public function setCi($ci) 
{ 
    $this->ci = $ci; 

    return $this; 
} 

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

/** 
* Set fechaNac 
* 
* @param \DateTime $fechaNac 
* @return DatUsuario 
*/ 
public function setFechaNac($fechaNac) 
{ 
    $this->fechaNac = $fechaNac; 

    return $this; 
} 

/** 
* Get fechaNac 
* 
* @return \DateTime 
*/ 
public function getFechaNac() 
{ 
    return $this->fechaNac; 
} 

/** 
* Set telefono 
* 
* @param string $telefono 
* @return DatUsuario 
*/ 
public function setTelefono($telefono) 
{ 
    $this->telefono = $telefono; 

    return $this; 
} 

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

/** 
* Set celular 
* 
* @param string $celular 
* @return DatUsuario 
*/ 
public function setCelular($celular) 
{ 
    $this->celular = $celular; 

    return $this; 
} 

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

/** 
* Set email 
* 
* @param string $email 
* @return DatUsuario 
*/ 
public function setEmail($email) 
{ 
    $this->email = $email; 

    return $this; 
} 

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

/** 
* Set imagen 
* 
* @param string $imagen 
* @return DatUsuario 
*/ 
public function setImagen($imagen) 
{ 
    $this->imagen = $imagen; 

    return $this; 
} 

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

/** 
* Set estado 
* 
* @param boolean $estado 
* @return DatUsuario 
*/ 
public function setEstado($estado) 
{ 
    $this->estado = $estado; 

    return $this; 
} 

/** 
* Get estado 
* 
* @return boolean 
*/ 
public function getEstado() 
{ 
    return $this->estado; 
} 

/** 
* Set rol 
* 
* @param \gishay\bdBundle\Entity\DatRol $rol 
* @return DatUsuario 
*/ 
public function setRol(\gishay\bdBundle\Entity\DatRol $rol = null) 
{ 
    $this->rol = $rol; 

    return $this; 
} 

/** 
* Get rol 
* 
* @return \gishay\bdBundle\Entity\DatRol 
*/ 
public function getRol() 
{ 
    return $this->rol; 
} 

/** 
* Set departamento 
* 
* @param \gishay\bdBundle\Entity\ClaDepartamento $departamento 
* @return DatUsuario 
*/ 
public function setDepartamento(\gishay\bdBundle\Entity\ClaDepartamento $departamento = null) 
{ 
    $this->departamento = $departamento; 

    return $this; 
} 

/** 
* Get departamento 
* 
* @return \gishay\bdBundle\Entity\ClaDepartamento 
*/ 
public function getDepartamento() 
{ 
    return $this->departamento; 
} 

/** 
* Set cargoUsuario 
* 
* @param \gishay\bdBundle\Entity\DatCargoUsuario $cargoUsuario 
* @return DatUsuario 
*/ 
public function setCargoUsuario(\gishay\bdBundle\Entity\DatCargoUsuario $cargoUsuario = null) 
{ 
    $this->cargoUsuario = $cargoUsuario; 

    return $this; 
} 

/** 
* Get cargoUsuario 
* 
* @return \gishay\bdBundle\Entity\DatCargoUsuario 
*/ 
public function getCargoUsuario() 
{ 
    return $this->cargoUsuario; 
} 

/** 
* Set profesion 
* 
* @param \gishay\bdBundle\Entity\ClaProfesion $profesion 
* @return DatUsuario 
*/ 
public function setProfesion(\gishay\bdBundle\Entity\ClaProfesion $profesion = null) 
{ 
    $this->profesion = $profesion; 

    return $this; 
} 

/** 
* Get profesion 
* 
* @return \gishay\bdBundle\Entity\ClaProfesion 
*/ 
public function getProfesion() 
{ 
    return $this->profesion; 
} 

}

mir jemand bitte helfen .. :)

Sry ... das ist mein ORM ist in YML DatUsuario

gishay\bdBundle\Entity\DatUsuario: 
type: entity 
table: dat_usuario 
indexes: 
    dat_usuario_FKIndex1: 
     columns: 
      - rol_id 
    dat_usuario_FKIndex2: 
     columns: 
      - departamento_id 
    dat_usuario_FKIndex3: 
     columns: 
      - cargo_usuario_id 
    dat_usuario_FKIndex4: 
     columns: 
      - profesion_id 
id: 
    id: 
     type: integer 
     nullable: false 
     unsigned: true 
     id: true 
     generator: 
      strategy: IDENTITY 
fields: 
    username: 
     type: string 
     nullable: false 
     length: 25 
     fixed: false 
    password: 
     type: string 
     nullable: false 
     length: 255 
     fixed: false 
    nombre: 
     type: string 
     nullable: false 
     length: 45 
     fixed: false 
    paterno: 
     type: string 
     nullable: true 
     length: 45 
     fixed: false 
    materno: 
     type: string 
     nullable: true 
     length: 45 
     fixed: false 
    ci: 
     type: string 
     nullable: false 
     length: 15 
     fixed: false 
    fechaNac: 
     type: date 
     nullable: true 
     column: fecha_nac 
    telefono: 
     type: string 
     nullable: true 
     length: 10 
     fixed: false 
    celular: 
     type: string 
     nullable: true 
     length: 10 
     fixed: false 
    email: 
     type: string 
     nullable: true 
     length: 45 
     fixed: false 
    imagen: 
     type: string 
     nullable: true 
     length: 100 
     fixed: false 
    estado: 
     type: boolean 
     nullable: true 
manyToOne: 
    rol: 
     targetEntity: DatRol 
     inversedBy: DatUsuario 
     joinColumns: 
      rol_id: 
       referencedColumnName: id 
     orphanRemoval: false 
    departamento: 
     targetEntity: ClaDepartamento 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
      departamento_id: 
       referencedColumnName: id 
     orphanRemoval: false 
    cargoUsuario: 
     targetEntity: DatCargoUsuario 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
      cargo_usuario_id: 
       referencedColumnName: id 
     orphanRemoval: false 
    profesion: 
     targetEntity: ClaProfesion 
     cascade: { } 
     mappedBy: null 
     inversedBy: null 
     joinColumns: 
      profesion_id: 
       referencedColumnName: id 
     orphanRemoval: false 
lifecycleCallbacks: { } 

Das ist mein DatRol

gishay\bdBundle\Entity\DatRol: 
type: entity 
table: dat_rol 
id: 
    id: 
     type: integer 
     nullable: false 
     unsigned: true 
     id: true 
     generator: 
      strategy: IDENTITY 
fields: 
    rol: 
     type: string 
     nullable: false 
     length: 50 
     fixed: false 
    abreviacion: 
     type: string 
     nullable: true 
     length: 50 
     fixed: false 
    estado: 
     type: boolean 
     nullable: true 
oneToMany: 
    DatUsuario: 
     targetEntity: DatUsuario 
     mappedBy: rol 
     fetch: EXTRA_LAZY 
lifecycleCallbacks: { } 

Danke für die Antwort g wieder, ... aber in meinem Bearbeitungsformular sind die Felder Rol, Profesion, etc ... leer ohne Auswahl ... und wieder brauche ich wählen ... Ich habe versucht mit Class DatRol ... Die anderen sind noch nicht ... Ich brauche das in den Feldern selektiere das ausgewählt die Daten der Datenbank ... danke

+0

Ich habe keine Beziehungen und Spaltennamen Ihres Unternehmens sehen. Wenn Sie keine PHP-Annotationen verwenden, müssen Sie 'yml' verwenden. Können Sie es auch posten? –

+0

Es tut mir leid .. Ich vergesse, die Beziehungen zu setzen .. aber die Beziehungen sind schon .. schau es bitte .... @ V-Light – JuanquiMon

Antwort

1

Ok, jetzt sehe ich.

Alle Ihre Beziehungen sind unidirektional, da Sie mappedBy: null und inversedBy: null haben, deshalb, Beziehungen Setup manualy wie

symfony denkt Sie
// manual relations 
$datUsuario->setRol($yourRoleEntity); 
$datUsuario->setDepartamento($yourRoleEntity); 
// and so on.. 
// but I think you don't want that.... 

// form 
$editForm = $this->createForm('gishay\bdBundle\Form\DatUsuarioType', $datUsuario); 

was auch möglich ist - Sie haben nur keine Datensätze in Ihrer DatRol, ClaDepartamento, DatCargoUsuario Tabellen noch. Aus diesem Grund sind die Dropdown-Listen leer. Wenn das der Fall ist, fügen Sie zuerst einige Daten hinzu!

Aber zurück zu Ihren Beziehungen ...

Check this great reference

Wie Sie sehen können Sie

manyToOne: 
    rol: 
     targetEntity: DatRol 
     cascade: { 'persist' } # Play around with other settings... 
     #remove this since it's incorret. you can't have both! 
     #mappedBy: null 
     inversedBy: datUsario 
     joinColumns: 
      rol_id: 
       referencedColumnName: id 
     orphanRemoval: false 

    # do the same for all others manyToOne relations 

erklären sollte, wenn es fertig ist, gehen Sie zu Ihrem DatRol, ClaDepartamento, DatCargoUsuario und ClaProfesion und bearbeiten Sie Ihre oneToMany Beziehungen. Entfernen inversedBy und ADD mapeedBy

wie:

oneToMany: 
    datUsario: 
     targetEntity: DatUsuario 
     mappedBy: rol 
     fetch: EXTRA_LAZY 

in allen anderen das gleiche tun ...

Rule-Of-Thumb:

  • inversedBy ist immer auf der Suche Seite wo FREMDSCHLÜSSEL ist (manyToMany Ausnahme ist, siehe ormcheatsheet für mehr Details)
  • mappedBy auf der anderen Seite

für weitere Informationen die ormcheatsheet von oben überprüfen und Doctrine's One-To-Many, Bidirectional

+0

ich update wieder, schaue es bitte V-Light ... ich erwartet für eure Kommentare ... – JuanquiMon

+0

liebe V-Ligth, ich habe es wieder versucht und ich habe meine bidirektionale Beziehung ... aber, das Problem geht weiter .. meine Klasse rol, profesion, etc, Habe Aufzeichnungen ... ich denke, das Problem ist in Das Formular .. Ich habe gerade den Code erneut aktualisiert, bitte überprüfen Sie – JuanquiMon

+1

Sehr geehrte V-Light, ... Es ist fertig .. Sie hatten den ganzen Grund, ich neu zugeordnet, nachdem die Änderungen und jetzt, wenn es funktioniert ... die Schlüssel war die Kaskade: ["persist"], damit, wenn es richtig funktioniert .. vielen Dank ... ich möchte dir 5 Sterne geben !!! .. =) – JuanquiMon

Verwandte Themen