我正在做一个简单的票据管理系统,我有两个实体。User
旗下 id, email, roles[] (Admin, Technician or Client)
和 username, password, tickets[] (which are all the tickets the client has submitted)
.我有一个 TicketFormType
类,让我可以创建新的票据,并为其分配 a technician to that ticket
,这里是它的代码。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'label' => 'Title',
'attr' => ['placeholder' => 'Ticket title']
])
->add('priority', ChoiceType::class, [
'multiple' => false,
'choices' => [
'Very High' => 5,
'High' => 4,
'Medium' => 3,
'Low' => 2,
'Very Low' => 1
]
])
->add('body')
->add('assignmentDate')
->add('technician') // this field gets populated with all users including those who don't have ROLE_TECHNICIAN
->add('customer')
;
}
现在在我的数据库结构中,我有 ticket table
这些领域 id technician_id customer_id title priority body assignment_date
哪儿 technician_id
是FK到PK中的table user
我的问题是,技术员字段是一个下拉菜单,它被填充了所有用户的 User table
包括那些没有 ROLE_TECHNICIAN
. 我如何解决这个问题?
NOTE: I store all technicians, clients, admins in table Users
你可以用一个QueryBuilder这样的。
$builder
->add('title', TextType::class, [
'label' => 'Title',
'attr' => ['placeholder' => 'Ticket title']
])
->add('priority', ChoiceType::class, [
'multiple' => false,
'choices' => [
'Very High' => 5,
'High' => 4,
'Medium' => 3,
'Low' => 2,
'Very Low' => 1
]
])
->add('body')
->add('assignmentDate')
->add('technician') // this field gets populated with all users including those who don't have ROLE_TECHNICIAN
->add('technician', EntityType::class, [
'class' => User::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->andWhere('u.ROLE LIKE :role')
->setParameter('role', '%\'ROLE_TECHNICIAN\'%');
},
'choice_label' => 'username',
])
->add('customer')
;
你必须根据你的需要来调整这个。