Symfony 数据转换和更新表单数据

Symfony Data Transform and Update Form With The Data

2个实体之间存在一对多关系。首先是用户实体,其次是域实体。一个用户可以拥有多个域。

这是用户实体(删除了其他字段,因为与主题无关):

class Users extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="\UsersBundle\Entity\UserDomains" , mappedBy="user" , cascade={"all"})
     */
    protected $domains;
    /**
     * Add domain
     *
     * @param \UsersBundle\Entity\UserDomains $domain
     *
     * @return Users
     */
    public function addDomain(\UsersBundle\Entity\UserDomains $domain)
    {
        $this->domains[] = $domain;

        return $this;
    }

    /**
     * Remove domain
     *
     * @param \UsersBundle\Entity\UserDomains $domain
     */
    public function removeDomain(\UsersBundle\Entity\UserDomains $domain)
    {
        $this->domains->removeElement($domain);
    }
}

这是 UserDomains 实体(一些字段已被删除):

class UserDomains
{
    public function __construct() {
        $this->setCreatedAt(new \DateTime());
    }

    public function __toString()
    {
        return $this->name;
    }

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="AffiliateBundle\Entity\Users", inversedBy="domains")
     * @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="CASCADE")
     */
    private $user;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set user
     *
     * @param integer $user
     *
     * @return UserDomains
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return integer
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return UserDomains
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

注册用户到系统时必须添加域。我有注册表格的 RegisterType。这种类型的表单 class 有 DataTransformer 用于添加注册用户的域。

RegisterType class 在这里:

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fullName', 'text', array('attr' => array('class' => 'form-control'), 'label' => 'user_register.fullname', 'translation_domain' => 'UsersBundle'))
            ->add('domains', 'text', array('attr' => array('class' => 'form-control select2', 'id' => 'select2_sample1')))
        ;

        $builder->get('domains')->addModelTransformer(new CallbackTransformer(
            function(){
                return '';
            },
            function($data){

                $arrCollection = new ArrayCollection();

                if (strpos($data, ",") !== false) {
                    $expData = explode(',', $data);
                    foreach ($expData as $domain) {
                        $domainObj = new UserDomains();
                        $domainObj->setName($domain);
                        $domainObj->setEnabled(true);
                        $arrCollection->add($domainObj);
                    }
                } else if (!empty($data)) {
                    $domain = new UserDomains();
                    $domain->setName($data);
                    $domain->setEnabled(true);
                    $arrCollection->add($domain);
                }

                return $arrCollection;
            }
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyBundle\Entity\Users',
            'intention'  => 'registration',
        ));
    }

    public function getParent()
    {
        return 'fos_user_registration';

        // Or for Symfony < 2.8
        // return 'fos_user_registration';
    }

    public function getName()
    {
        return 'user_registration';
    }
}

所以任何人都填写注册表并提交。用户创建了域并将域添加到 user_domains table 但没有更新必须是新用户 ID 的 user 字段。如何自动执行此操作?或者您是否有任何想法用新用户的 ID 更新 user_domains table 的 user 列?

感谢您对所有 Whosebug 的帮助:)

像这样将 __construct 添加到 RegistrationType:

class RegistrationType extends AbstractType
{
    private $user;

    public  function  __construct(Users $user) {
        $this->user = $user;
    }

像这样将 setUser 函数添加到您的 DataTransform 中:

$domainObj->setUser($this->user);
//and
$domain->setUser($this->user);

最后像这样更新你的控制器:

$form = $this->createForm(new RegistrationType($userEntity), $userEntity);