我是初学的symfony开发者。 我的表格有很大问题 我想将数据保存到我的表名称广告活动,如下所示:
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, exmple@email.com, New York, 2016-12-12 12:12:12
2, 1 , 1, test@emailer.com, New York, 2016-12-12 12:12:12
3, 1 , 2, proplayer@games.com, 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 :(得分:0)
是Ajax:
在您的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);
}
:
$("#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()并调整javascript即可满足您的需求。
希望这有帮助!