Php Symfony-许多人没有得到拯救
场景 我有两个实体通过Php Symfony-许多人没有得到拯救,php,symfony,Php,Symfony,场景 我有两个实体通过manytomy关系链接在一起 实体 用户 利息 因此,在用户的配置文件表单上,有一个名为兴趣的字段,该字段使用 现在,用户可以选择他们想要的任意多个兴趣,并且在保存后,doctrine在链接表中保存所选兴趣方面做得很好。当我重新加载配置文件页面时,我可以看到我已经选择的兴趣 问题 尽管表单字段与兴趣实体链接 $form->add('interest', EntityType::class, array(
manytomy
关系链接在一起
实体
用户
利息
兴趣
的字段,该字段使用
现在,用户可以选择他们想要的任意多个兴趣,并且在保存后,doctrine在链接表中保存所选兴趣方面做得很好。当我重新加载配置文件页面时,我可以看到我已经选择的兴趣
问题
尽管表单字段与兴趣
实体链接
$form->add('interest', EntityType::class, array(
'class' => 'AppBundle\Entity\Interest',
'multiple' => true,
'expanded' => false,
'by_reference' => false)
在前端和后端的帮助下,用户还可以添加自己在兴趣
表中不存在的兴趣,以保存此信息。我已经准备好了此信息,用于检查用户提交的兴趣是否不存在于兴趣
表中。请添加这些兴趣,在这里我可以获得以下信息例外情况
Message
This value is not valid.
Origin
interest
Cause
Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[interest] = [0 => 1, 1 => 4, 2 => 7, 3 => www]
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "interest": Could not find all matching choices for the given values
Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Could not find all matching choices for the given values
这是事件订阅者代码
namespace AppBundle\Form\EventListener;
use AppBundle\Entity\Interest;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
class AddProfileFieldSubscriber implements EventSubscriberInterface
{
protected $authorizationChecker;
protected $em;
function __construct(AuthorizationChecker $authorizationChecker, EntityManager $em)
{
$this->authorizationChecker = $authorizationChecker;
$this->em = $em;
}
public static function getSubscribedEvents()
{
// Tells the dispatcher that you want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return array(
FormEvents::PRE_SUBMIT => 'onPreSubmit'
);
}
/**
* @param FormEvent $event
*/
public function onPreSubmit(FormEvent $event){
$interestTags = $event->getData();
$interestTags = $interestTags['interest'];
foreach($interestTags as $interestTag){
$interest = $this->em->getRepository('AppBundle:Interest')->findOneBy(array('id' => $interestTag));
if(!$interest){
$newInterest = new Interest();
$newInterest->setName($interestTag);
$this->em->persist($newInterest);
$this->em->flush();
}
}
}
}
尝试
我通过添加choice\u值
$form->add('interest', EntityType::class, array(
'class' => 'AppBundle\Entity\Interest',
'multiple' => true,
'expanded' => false,
'by_reference' => false,
'choice_value' => 'name'
)
);
我将事件订阅服务器中的查询从
$interest = $this->em->getRepository('AppBundle:Interest')->findOneBy(array('id' => $interestTag));
到
这在一开始工作得很好,但当我重新加载配置文件页面时,我的兴趣字段显示为空
它显示为空的原因是(我的假设)我认为它应该看起来像id=“select2-user\u interest-result-5z18-Education”
- 教育
- 历史记录
- 体系结构
- 企业家精神
- 技术
- 工程
我交叉检查了兴趣表中的数据,我可以看到用户添加的新兴趣,这些兴趣以前不存在,因此它可以工作,但在前端没有显示。出于好奇,我删除了choice\u值
,然后重新加载个人资料页面,我可以看到新的兴趣
如果有任何事情能将我推向正确的方向,并让我知道我缺少什么,以及如何使其发挥作用,我将不胜感激。您不能这样做,因为
EntityType
extendChoiceType
不允许在选项列表中添加新值
您应该像在烹饪书中一样使用CollectionType
要在代码中进行适当的选择,您还必须在视图中插入所有兴趣的完整列表以及表单
那么您的视图应该如下所示:
<select name='{{ form.interest.vars.full_name }}' id="{{ form.interest.vars.id }}" class='select2'>
{% for interestList as interestItem %}
<option value='{{ interestItem.id }}' {% if some_logic_to_check_whether_the_item_is_selected %}selected='selected'{% endif %} >{{ interestItem.name }}</option>
{% endfor %}
</select>
{作为interestItem%列出的interestList的%
{{interestItem.name}
{%endfor%}
您不能这样做,因为EntityType
extendChoiceType
不允许在选项列表中添加新值
您应该像在烹饪书中一样使用CollectionType
要在代码中进行适当的选择,您还必须在视图中插入所有兴趣的完整列表以及表单
那么您的视图应该如下所示:
<select name='{{ form.interest.vars.full_name }}' id="{{ form.interest.vars.id }}" class='select2'>
{% for interestList as interestItem %}
<option value='{{ interestItem.id }}' {% if some_logic_to_check_whether_the_item_is_selected %}selected='selected'{% endif %} >{{ interestItem.name }}</option>
{% endfor %}
</select>
{作为interestItem%列出的interestList的%
{{interestItem.name}
{%endfor%}
我实际上可以使用我在尝试中提到的方法添加,但缺点是select2字段显示为空,但是在数据库中我可以看到新数据被添加并链接到用户。我实际上可以使用我在尝试中提到的方法添加,但缺点是select2字段显示为空,但在数据库中,我可以看到新数据被添加并链接到用户
<select name='{{ form.interest.vars.full_name }}' id="{{ form.interest.vars.id }}" class='select2'>
{% for interestList as interestItem %}
<option value='{{ interestItem.id }}' {% if some_logic_to_check_whether_the_item_is_selected %}selected='selected'{% endif %} >{{ interestItem.name }}</option>
{% endfor %}
</select>