Symfony3 FOS UserBundle 如何加载选择作为实体

Symfony3 FOS UserBundle how to load choice as entity

我正在将 Symfony3.1 与 FOS UsersBundle 一起使用,我希望将一些添加的字段作为特定实体加载。

RegistrationType我有

->add('country', ChoiceType::class, array(
    'label' => 'label.country',
    'required' => false,
    'placeholder' => 'label.select_country',
    'choices'  => array(
        'France' => '7v8tqr',
    ),
    ))

在我的实体中User我有

/**
 * @ORM\OneToOne(targetEntity="Country")
 * @ORM\JoinColumn(name="country", referencedColumnName="short")
 */
protected $country;

我无法使用 EntityType,因为它会加载每个可用的实体,而且我对相当大的省份和城市使用相同类型的字段(我使用 javascript 管理它们的内容) .

当我加载注册用户时,国家字段作为国家实体提供服务,但是当我注册新用户或修改现有用户时,我只有字符串 "short",这会导致错误 Expected value of type "AppBundle\Entity\Country" for association field "AppBundle\Entity\User#$country", got "string" instead..

有解决办法吗?

感谢@mcriecken 引导我走向正确的方向,我已经使用 EventListener

实现了以下解决方案

services.yml

app_user.registration:
    class: AppBundle\EventListener\UserRegistrationListener
    arguments: ['@doctrine.orm.entity_manager']
    tags:
        - { name: kernel.event_subscriber }

和 EventListener UserRegistrationListener.php

<?php

namespace AppBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Doctrine\ORM\EntityManager;

class UserRegistrationListener implements EventSubscriberInterface
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        );
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        $form = $event->getForm()->getData();

        //Gets the locations
        $form->setCountry($this->getCountry($form->getCountry()));
        $form->setProvince($this->getProvince($form->getProvince()));
        $form->setCity($this->getCity($form->getCity()));
    }

    //Loads the country as an entity
    public function getCountry($short)
    {
        if ($short == null) return null;

        $repository = $this->em->getRepository('AppBundle:Country');
        return $repository->findOneByShort($short);
    }

    //Loads the province as an entity
    public function getProvince($short)
    {
        if ($short == null) return null;

        $repository = $this->em->getRepository('AppBundle:Province');
        return $repository->findOneByShort($short);
    }

    //Loads the city as an entity
    public function getCity($short)
    {
        if ($short == null) return null;

        $repository = $this->em->getRepository('AppBundle:City');
        return $repository->findOneByShort($short);
    }

}

最后我的 FOS 用户对象包含国家、省和城市作为对象,它可以保存到数据库:-)