gpt4 book ai didi

php - Symfony 表单将选择的用户兴趣存储到 UserInterests

转载 作者:行者123 更新时间:2023-12-02 03:07:09 25 4
gpt4 key购买 nike

我的目标是使用 Symfony 表单允许用户选择多个兴趣并根据用户的关系保存这些兴趣。

我有三个实体来映射和存储此数据:

User
- id
- name

Interest
- id
- name

UserInterest
- id
- user_id (FK ManyToOne user.id)
- interest_id (FK ManyToOne interest.id)

我正在努力寻找最动态的Symfony方式来处理用户兴趣并将其保存到 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);
}
}

问题

我目前面临的问题是,在提交表单后,这会创建链接到用户但具有多个选定UserInterest实体的实例兴趣作为数组而不是多个UserInterest

我怎样才能以正确的 Symfony 方式做到这一点,以允许一个表单和简单的 Controller 逻辑,以便用户可以加载页面,从多个复选框类型中选择他们的兴趣,单击“保存”,然后当重新加载表单时,他们会自动填充为之前的选择?

最佳答案

您的第一个解决方案对于您想要实现的目标来说有点过于复杂。如果您只想让用户选择一些兴趣,则不需要 UserInterest 实体。

我能想到的专用于该关系的新实体的唯一用途是该关系是否带有您想要保留的一些额外属性,例如关系开始的日期,可能是顺序或优先级或您需要的任何内容。如果您要存储的唯一数据是 userId 和interestId,那么新实体不会给您带来太多,只会带来复杂性。您也不需要 CollectionType(无论如何您都不会在此表单上创建新的兴趣)。使用用户和兴趣之间简单的多对多关系

 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,实体类型的类是 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();

关于php - Symfony 表单将选择的用户兴趣存储到 UserInterests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56884249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com