2016-12-21 1 views
2

Ich bin Anfänger symfony Entwickler. Ich habe großes Problem mit Form ich mag Daten auf meine Tabellennamen Kampagnen speichern, die wie folgt aussehen:Symfony 2 Formular mit mehreren Tabelle wählen Sie Typ

contacts_groups: 
id, user_id, name, description, created 
für Empfänger-System

Campaigns: 
id, user_id, recipients, message, sender, type, created 

und die nächsten

Ich habe 2 Tabellen in der Basis

und

contacts: 
id, user_id, group_id, email, city, created. 

lassen jetzt ‚S gehen davon aus, dass meine folgenden Tabellen Datensätze:

contacts_groups: 
     id, user_id, name, description, created 
     1, 1, Test Group, Newest recipients, 2016-20-02 14:24:00 
     2, 1, Awesome Group, Pro players, 2016-10-02 11:22:41 
    contacts: 
     id, user_id, group_id, email, city, created 
     1, 1 , 1, [email protected], New York, 2016-12-12 12:12:12 
     2, 1 , 1, [email protected], New York, 2016-12-12 12:12:12 
     3, 1 , 2, [email protected], New York, 2016-12-12 12:12:12 

Jetzt i eine Form mit zwei Tabellen tun wollen zeichnet Beispiel:

TextField => text for sender name row 
ChoiceType => select with options with contacts_groups records 
ChoiceType => select with options with contacts where group_id == choosed contacts_groups record above. 

ich habe diesen Code:

class CampaignsType extends AbstractType{ 


public function buildForm(FormBuilderInterface $builder, array $options) { 
    $Campaigns = $options['data']; 
    $user  = $Campaigns->tmpusr; // variable transferred from the controller (logged user id) 

    $builder 
      ->add('sender', TextType::class, array(
      'trim'  => true, 
      'label' => 'Odbiorca', 
      'label_attr' => array('class' => 'col-sm-2 control-label'), 
      'attr' => array('class' => 'form-control', 'maxlength' => '11') 
       )) 
     ->add('recipients', EntityType::class, array(
      'label' => 'Grupa odbiorców', 
      'label_attr' => array('class' => 'col-sm-2 control-label'), 
      'attr' => array('class' => 'form-control'), 
      'class' => 'MyAppPanelBundle:ContactsGroups', 
      'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($user) { 
       return $er->createQueryBuilder('u') 
        ->select('u') 
        ->add('where', 'u.user_id = ' . $user) 
        ->orderBy('u.id', 'DESC'); 

      }, 
      'choice_value' => 'id', 
      'choice_label' => 'name', 
       ));  
} 

Weiß jemand, wie man das nächste Feld mit Kontakten aus der Datenbank hinzufügt, wobei group_id dasselbe ist wie die ID der ausgewählten Gruppe?

Danke an alle.

+0

Um dies zu erreichen, müssen Sie Ajax verwenden, um eine Anfrage mit ausgewählter 'group' zu senden und als Antwort eine Liste von' contacts' [doc chapter] (http://symfony.com/doc/current/form/dynamic_form_modification.html) –

Antwort

0

Ja von Ajax:

  • Ad dies in Ihrem CampaignsType:

    ->add('contact', ChoiceType::class, array(
         'required' => true, 
         'placeholder' => 'choose contact' 
        )) 
    
  • Ihrer Ansicht:

       <div class="form-group"> 
            {{ form_label(form.contact, label|default(null), { 'label_attr': { 'class': 'font-secondary' } }) }} 
            {{ form_widget(form.contact, {'attr' : {'class' : 'form-control'}})}} 
            {{ form_errors(form.contact) }} 
           </div> 
    
  • in Ihrem Controller:

`

/** 
* @Route("/select/contact", name="_select_contact") 
*/ 
public function contactsAction(Request $request) { 
    $group_id = $request->request->get('group_id'); 
    $em = $this->getDoctrine()->getManager(); 
    $contacts = $em->getRepository('AppBundle:Contact')->getContacts($group_id); 
    return new JsonResponse($contacts); 
} 
  • in Ihrer js-Datei:

      $("#campaigns_group").change(function() { 
          var data = { 
           group_id: $(this).val() 
          }; 
          //$('form').spin(); 
          $.ajax({ 
           type: 'post', 
           url: Routing.generate('_select_contact'), 
           data: data 
          }).done(function (response) { 
           var $contacts_selector = $('#campaigns_contact'), 
            contacts = response.contact; 
           $contacts_selector.html('<option>choise contact</option>'); 
           for (var i = 0, total = contacts.length; i < total; i++) { 
            $contacts_selector.append('<option value="' + contacts[i].id + '">' + contacts[i].email + '</option>'); 
           } 
    
          }).fail(function() { 
           alert("error"); 
          }).always(function() { 
           //$('form').find('.spinner').hide() 
          }); 
         }); 
    

Erstellen Sie einfach das getContacts() in dem in Kontakt Repository und ajust JavaScript Ihre Bedürfnisse zu füllen.

Hoffe diese Hilfe!

+0

Danke, Bruder! :) –

Verwandte Themen