2016-12-21 27 views
2

我是初學者symfony開發人員。 我有形式 我想將數據保存到我的表名廣告活動,看起來像大問題:Symfony 2表格與多表選擇類型

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

和下

我在基地2個表收件人系統

contacts_groups: 
id, user_id, name, description, created 

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

現在就讓我們假定我的表有如下記錄:

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 

現在我想要做一個表格2個表中的記錄 例子:

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. 

我有代碼:

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', 
       ));  
} 

是否有人知道如何添加下一個字段與數據庫中的聯繫人,其中group_id將與選定組的ID相同?

謝謝大家。

+0

爲了達到這個目的,你需要使用ajax發送帶有選定'group'的請求並作爲響應獲取'contacts'列表[doc chapter](http://symfony.com/doc/current/form/dynamic_form_modification.html ) –

回答

0

是阿賈克斯:

  • 廣告這個在您的CampaignsType:

    ->add('contact', ChoiceType::class, array(
         'required' => true, 
         'placeholder' => 'choose contact' 
        )) 
    
  • 在你看來

       <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> 
    
  • 在你的控制器

`

/** 
* @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); 
} 
  • 在您的js文件:

      $("#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() 
          }); 
         }); 
    

只需創建一個在您接觸的庫中的getContacts()和ajust的JavaScript,以滿足您的需要。

希望得到這個幫助!

+0

謝謝你兄弟! :) –