Symfony Form 将用户兴趣的选择存储到 UserInterests
Symfony Form to store a selection of User Interests to UserInterests
我的目标是使用 Symfony 表单允许用户选择多个兴趣并将这些兴趣与他们的用户相关联地保存。
我有三个实体来映射和存储此数据:
User
- id
- name
Interest
- id
- name
UserInterest
- id
- user_id (FK ManyToOne user.id)
- interest_id (FK ManyToOne interest.id)
我正在努力寻找最动态的 Symfony 方法来处理 User
的 Interest
并将其保存到 UserInterest
实体。
InterestsController.php
public function interests(Request $request)
{
$error = null;
$userInterests = new UserInterest();
$userInterests->setUser($this->getUser());
$form = $this->createForm(UserAccountInterests::class, $userInterests);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->entityManager->persist($userInterests);
$this->entityManager->flush();
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
$parameters = [
'error' => $error,
'user_interests_form' => $form->createView()
];
return $this->render('user/interests.html.twig', $parameters);
}
UserInterestsType.php
class UserInterestsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('interest', EntityType::class, [
'class' => Interest::class,
'choice_label' => 'name',
'expanded' => true,
'multiple' => true
])
->add('update', SubmitType::class);
}
}
问题
我目前面临的问题是,在提交表单时,这会创建链接到 user
的 UserInterest
实体的实例化,但具有多个 selected Interest
作为数组而不是多个 UserInterest
.
我怎样才能以正确的 Symfony 方式做到这一点,以允许一种形式和简单的控制器逻辑,以便用户可以加载页面,select 他们的兴趣来自多个复选框类型,单击保存然后何时表格已重新加载,它们会像之前一样自动填充 selection?
你的第一个解决方案对于你想要实现的目标来说有点太复杂了。如果您只想让用户 select 有一些兴趣,则不需要 UserInterest 实体。
对于专用于该关系的新实体,我能想到的唯一用途是该关系是否带有您想要保留的一些额外属性,例如关系开始的日期,可能是订单或优先级或任何您想要保留的属性需要。如果您要存储的唯一数据是 userId 和 interestId,那么新实体不会给您带来太多,但会带来复杂性。
您也不需要 CollectionType(无论如何您都不会对此表单产生新的兴趣)。在 User 和 Interest
之间建立一个简单的 ManyToMany 关系
User:
type: entity
manyToMany:
interests:
targetEntity: Interest
inversedBy: users
joinTable:
## name will be the name of the table storing your entities 's relations
## Beware not to give it a name already defined for an entity table (like your UserInterest wich you don't need anymore)
name: users_interests
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
interest_id:
referencedColumnName: id
Interest:
type: entity
manyToMany:
users:
targetEntity: User
mappedBy: interests
然后您可以在表单中使用简单的多个 EntityType,其中 data_class 是 User 并且 class 您的实体类型是 Interest:
class UserInterestsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('interests', EntityType::class, [
'class' => Interest::class,
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
])
->add('update', SubmitType::class);
}
}
然后假设您想要检索共享特定兴趣的用户或特定用户的兴趣,您将在现有的用户和兴趣存储库中进行此类查询:
// UserRepository: All users sharing one (or more) interests
$em = $this->getEntityManager();
$repository = $em->getRepository('YourNamespace:User');
$query = $repository->createQueryBuilder('u')
->innerJoin('u.interests', 'i')
->where('i.id = :interest_id')
->setParameter('interest_id', 5) // just for the example
->getQuery()->getResult();
// InterestRepository: All interests of one user
$em = $this->getEntityManager();
$repository = $em->getRepository('YourNamespaceYourBundle:Interest');
$query = $repository->createQueryBuilder('i')
->innerJoin('i.users', 'u')
->where('u.id = :user_id')
->setParameter('user_id', 1)
->getQuery()->getResult();
我的目标是使用 Symfony 表单允许用户选择多个兴趣并将这些兴趣与他们的用户相关联地保存。
我有三个实体来映射和存储此数据:
User
- id
- name
Interest
- id
- name
UserInterest
- id
- user_id (FK ManyToOne user.id)
- interest_id (FK ManyToOne interest.id)
我正在努力寻找最动态的 Symfony 方法来处理 User
的 Interest
并将其保存到 UserInterest
实体。
InterestsController.php
public function interests(Request $request)
{
$error = null;
$userInterests = new UserInterest();
$userInterests->setUser($this->getUser());
$form = $this->createForm(UserAccountInterests::class, $userInterests);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->entityManager->persist($userInterests);
$this->entityManager->flush();
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
$parameters = [
'error' => $error,
'user_interests_form' => $form->createView()
];
return $this->render('user/interests.html.twig', $parameters);
}
UserInterestsType.php
class UserInterestsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('interest', EntityType::class, [
'class' => Interest::class,
'choice_label' => 'name',
'expanded' => true,
'multiple' => true
])
->add('update', SubmitType::class);
}
}
问题
我目前面临的问题是,在提交表单时,这会创建链接到 user
的 UserInterest
实体的实例化,但具有多个 selected Interest
作为数组而不是多个 UserInterest
.
我怎样才能以正确的 Symfony 方式做到这一点,以允许一种形式和简单的控制器逻辑,以便用户可以加载页面,select 他们的兴趣来自多个复选框类型,单击保存然后何时表格已重新加载,它们会像之前一样自动填充 selection?
你的第一个解决方案对于你想要实现的目标来说有点太复杂了。如果您只想让用户 select 有一些兴趣,则不需要 UserInterest 实体。
对于专用于该关系的新实体,我能想到的唯一用途是该关系是否带有您想要保留的一些额外属性,例如关系开始的日期,可能是订单或优先级或任何您想要保留的属性需要。如果您要存储的唯一数据是 userId 和 interestId,那么新实体不会给您带来太多,但会带来复杂性。 您也不需要 CollectionType(无论如何您都不会对此表单产生新的兴趣)。在 User 和 Interest
之间建立一个简单的 ManyToMany 关系 User:
type: entity
manyToMany:
interests:
targetEntity: Interest
inversedBy: users
joinTable:
## name will be the name of the table storing your entities 's relations
## Beware not to give it a name already defined for an entity table (like your UserInterest wich you don't need anymore)
name: users_interests
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
interest_id:
referencedColumnName: id
Interest:
type: entity
manyToMany:
users:
targetEntity: User
mappedBy: interests
然后您可以在表单中使用简单的多个 EntityType,其中 data_class 是 User 并且 class 您的实体类型是 Interest:
class UserInterestsType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('interests', EntityType::class, [
'class' => Interest::class,
'choice_label' => 'name',
'expanded' => true,
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
])
->add('update', SubmitType::class);
}
}
然后假设您想要检索共享特定兴趣的用户或特定用户的兴趣,您将在现有的用户和兴趣存储库中进行此类查询:
// UserRepository: All users sharing one (or more) interests
$em = $this->getEntityManager();
$repository = $em->getRepository('YourNamespace:User');
$query = $repository->createQueryBuilder('u')
->innerJoin('u.interests', 'i')
->where('i.id = :interest_id')
->setParameter('interest_id', 5) // just for the example
->getQuery()->getResult();
// InterestRepository: All interests of one user
$em = $this->getEntityManager();
$repository = $em->getRepository('YourNamespaceYourBundle:Interest');
$query = $repository->createQueryBuilder('i')
->innerJoin('i.users', 'u')
->where('u.id = :user_id')
->setParameter('user_id', 1)
->getQuery()->getResult();