Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Symfony EntityType 默认值设置:会话数据与托管实体处理指南_创想鸟

Symfony EntityType 默认值设置:会话数据与托管实体处理指南

Symfony EntityType 默认值设置:会话数据与托管实体处理指南

在symfony中为entitytype表单字段设置默认选中值,特别是当该值来源于会话中的实体时,常会遇到实体非托管的挑战。本文将详细阐述如何正确利用`data`选项进行预选,处理会话中分离(detached)的实体,并探讨`choice_value`的正确用途及客户端javascript的替代方案,确保表单数据准确回显。

理解 Symfony EntityType 与默认值设置

Symfony的EntityType表单字段类型旨在将HTML 元素与Doctrine实体关联起来。当我们需要为这类字段设置一个默认的选中值时,通常会使用data选项。然而,一个常见的误解和由此引发的问题是,data选项期望接收一个Doctrine托管(managed)实体,或者一个由托管实体组成的集合。如果传入的实体是“分离”(detached)状态(例如,从会话中反序列化而来但未重新附加到EntityManager),Symfony会抛出错误,提示实体未被管理。

使用 data 选项进行服务器端预选

data选项是设置EntityType默认选中值的标准方法。其关键在于确保传递给它的实体是当前Doctrine EntityManager所管理的。

1. 传递托管实体

最直接的方法是直接将一个从数据库中获取的、处于托管状态的实体传递给data选项。

use AppEntityEtude;use SymfonyBridgeDoctrineFormTypeEntityType;use DoctrineORMEntityManagerInterface; // 注入或通过服务获取// 假设 $entityManager 已经被注入到你的 FormType 或 Controller 中// 假设 $selectedEtude 是一个从数据库中获取的托管 Etude 实体$builder->add('etude', EntityType::class, [    'label' => 'Étude',    'class' => Etude::class,    'required' => false,    'data' => $selectedEtude, // 必须是托管实体]);

2. 处理会话中分离(Detached)的实体

当实体存储在会话中时,它们通常在被存储时序列化,并在从会话中取出时反序列化。反序列化后的实体对象是“分离”状态的,即它们不再与任何EntityManager关联。直接将这样的实体传递给data选项会导致类似“…passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?”的错误。

解决方案: 从会话中获取实体的唯一标识(通常是ID),然后使用EntityManager重新从数据库中加载该实体。这样获取的实体就是托管状态的。

示例:

首先,在控制器中准备好过滤数据,并确保从会话中获取的实体ID被正确提取。

// In your Controlleruse AppFormFilterActeType;use DoctrineORMEntityManagerInterface;use SymfonyComponentHttpFoundationRequest;use AppEntityEtude; // 确保导入 Etude 实体类class YourController extends AbstractController{    public function someAction(Request $request, PaginatorService $paginatorService, EntityManagerInterface $entityManager)    {        // ... 获取用户ID和路由        $usrId = $this->getUser()->getId(); // 假设你有一个获取当前用户的方法        $route = $request->attributes->get('_route');        // 获取默认过滤器和会话过滤器        $filters = array_merge(            $defaultFilter, // 假设 $defaultFilter 已经定义            $paginatorService->getFiltersFromSessionByContext($usrId, $route)        );        $preselectedEtude = null;        if (isset($filters['etude']) && $filters['etude'] instanceof Etude) {            // 从会话中获取的 Etude 实体是分离的,需要重新从数据库加载            // 确保 Etude 实体有一个 getId() 方法            $etudeId = $filters['etude']->getId();            if ($etudeId) {                // 使用 EntityManager 重新加载托管实体                $preselectedEtude = $entityManager->getRepository(Etude::class)->find($etudeId);            }        }        // 创建表单,并将托管实体作为选项传递        $filter_form = $this->createForm(FilterActeType::class, null, [            'filters' => $filters, // 仍然传递原始过滤器,FormType内部可能需要            'preselected_etude' => $preselectedEtude, // 传递托管实体        ]);        // ... 表单处理和渲染        if ($filter_form->isSubmitted() && $filter_form->isValid()) {            // 处理表单数据        }        return $this->render('your_template.html.twig', [            'filter_form' => $filter_form->createView(),        ]);    }}

然后,在你的FormType中,通过options获取这个托管实体,并将其赋值给data选项:

// In your FormType (e.g., FilterActeType)namespace AppForm;use AppEntityEtude;use SymfonyBridgeDoctrineFormTypeEntityType;use SymfonyComponentFormAbstractType;use SymfonyComponentFormFormBuilderInterface;use SymfonyComponentOptionsResolverOptionsResolver;use SymfonyContractsTranslationTranslatorInterface; // 假设需要翻译class FilterActeType extends AbstractType{    private $translator;    public function __construct(TranslatorInterface $translator)    {        $this->translator = $translator;    }    public function buildForm(FormBuilderInterface $builder, array $options): void    {        $builder->add('etude', EntityType::class, [            'label' => $this->translator->trans('Étude'),            'class' => Etude::class,            'required' => false,            'attr' => ['dyn-form-data' => 'cabinet,createur,destinataire'],            'data' => $options['preselected_etude'], // 使用控制器中加载的托管实体        ]);        // ... 其他字段    }    public function configureOptions(OptionsResolver $resolver): void    {        $resolver->setDefaults([            // ... 其他默认选项            'filters' => [], // 定义 filters 选项及其默认值            'preselected_etude' => null, // 定义 preselected_etude 选项及其默认值        ]);        // 确保 preselected_etude 选项可以是 Etude 实体或 null        $resolver->setAllowedTypes('preselected_etude', [Etude::class, 'null']);    }    // 原始的 getDataFromFilters 方法在此场景下不再直接用于 data 选项    // private function getDataFromFilters(array $options, string $field) { ... }}

注意事项:

data选项不接受匿名函数来查询实体。 匿名函数通常用于query_builder选项来限制可选实体列表,而不是提供默认值。直接将一个返回QueryBuilder的匿名函数赋给data是无效的。确保从会话中获取的实体ID是有效的,并且对应的实体确实存在于数据库中。

choice_value 选项的正确用途

choice_value选项的目的是定义在HTML 元素的value属性中应使用哪个实体属性。它不是用来设置默认选中值的。

例如,如果你希望 Apple 中的 value=”1″ 对应的是实体的 id 属性,那么你可以这样设置:

$builder->add('etude', EntityType::class, [    'label' => 'Étude',    'class' => Etude::class,    'required' => false,    'choice_label' => 'libelle', // 显示给用户的文本    'choice_value' => 'id',     // 作为  value 的属性]);

你也可以传递一个匿名函数给choice_value,该函数接收一个实体对象并返回其值:

$builder->add('etude', EntityType::class, [    'label' => 'Étude',    'class' => Etude::class,    'required' => false,    'choice_label' => 'libelle',    'choice_value' => function (?Etude $etude) {        return $etude ? $etude->getId() : '';    },]);

这只会影响HTML value属性的生成,而不会影响哪个选项被默认选中。

客户端 JavaScript 预选方案(替代方法)

在某些复杂或特定场景下,如果服务器端处理托管实体较为繁琐,或者需要基于更复杂的客户端逻辑进行预选,可以考虑使用JavaScript。

实现步骤:

控制器传递数据: 在控制器中,将需要预选的实体ID或其他标识符作为变量传递给Twig模板。

// In your Controller// ...return $this->render('your_template.html.twig', [        'filter_form' => $filter_form->createView(),        'preselectedEtudeId' => $preselectedEtude ? $preselectedEtude->getId() : null, // 传递ID]);

Twig模板渲染: 在Twig模板中,渲染表单字段,并通过JavaScript获取预选值。

{# your_template.html.twig #}{{ form_start(filter_form) }}    {{ form_row(filter_form.etude) }}    {# ... 其他字段 #}{{ form_end(filter_form) }}    document.addEventListener('DOMContentLoaded', function() {        var preselectedId = {{ preselectedEtudeId | json_encode | raw }}; // 获取预选ID        // 获取 select 元素,假设 'etude' 字段的完整ID是 'filter_acte_etude'        // 可以通过 form_widget(filter_form.etude).vars.id 获取准确ID        var etudeSelect = document.getElementById('{{ filter_form.etude.vars.id }}');         if (etudeSelect && preselectedId !== null) { // 检查 preselectedId 是否为 null

以上就是Symfony EntityType 默认值设置:会话数据与托管实体处理指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1325231.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
如何撤销 “make install” 的操作?
上一篇 2025年12月12日 11:49:09
WordPress外部PHP页面独立加载页脚:方法与注意事项
下一篇 2025年12月12日 11:49:21

相关推荐

发表回复

登录后才能评论
关注微信