PUGXMultiUserBundle 和注册表嵌入

PUGXMultiUserBundle and Registration form embedded

我使用 PUGXMultiUserBundle 因为我有两种类型的用户:CustomerUser 和 ShopUser。 ShopUser 与 Location(address,city...) 具有 OneToMany 关系。

我需要将所有数据仅从 RegistrationShopUserFormType 保存到表(商店和位置)。我怎样才能用 PUGX 做到这一点?

我试过使用Form embedded和form collection,但是没有很好的效果。 你有什么想法吗?

编辑

这里是代码:

表格/:

RegistrationShopUserFormType.php

<?php


namespace AppBundle\Form;
use AppBundle\Entity\ShopUser;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RegistrationShopUserFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('lastName')
            ->add('shopName')

        ;

    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

    public function getBlockPrefix()
    {
        return 'user_shop';
    }

    public function getName()
    {
        return $this->getBlockPrefix();
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => ShopUser::class,
        ));
    }

}

LocationFormType.php

<?php


namespace AppBundle\Form;

use AppBundle\Entity\Branch;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LocationFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('address')
                ->add('city')
                ->add('cap')
                ->add('lat')
                ->add('lng')
            ;
        $builder->add('shop', CollectionType::class, array(
            'entry_type' => RegistrationShopUserFormType::class
        ));
    }

    public function getBlockPrefix()
    {
        return 'location';
    }

    public function getName()
    {
        return $this->getBlockPrefix();
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Location::class,
        ));
    }


}

实体/: ShopUser.php(来自 PUGMultiUserBundle)-没有 getter 和 setter-

<?php

namespace AppBundle\Entity;


use Doctrine\ORM\Mapping as ORM;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;


/**
 * @ORM\Entity
 * @ORM\Table(name="shop")
 * @UniqueEntity(fields = "username", targetClass = "AppBundle\Entity\User", message="fos_user.username.already_used")
 * @UniqueEntity(fields = "email", targetClass = "AppBundle\Entity\User", message="fos_user.email.already_used")
 */
class ShopUser extends User
{
    /**
     * @ORM\Id
     *
     * @ORM\Column(type="string")
     */
    protected $id;

    /**
     * @ORM\Column(type="string",nullable=true)
     */
    protected $vat;


    /**
     * @ORM\Column(type="string")
     */
    protected $name;

    /**
     * @ORM\Column(type="string")
     */
    protected $lastName;

    /**
     * @ORM\Column(type="string")
     */
    protected $shopName;

    /**
     * @ORM\Column(type="string",nullable=true)
     */
    protected $logo;

}

Location.php - 没有一些 getter 和 setter-

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Location
 *
 * @ORM\Table(name="location", uniqueConstraints={@ORM\UniqueConstraint(name="id_UNIQUE", columns={"id"})}, indexes={@ORM\Index(name="fk_location_shop1_idx", columns={"shop_id"})})
 * @ORM\Entity
 */
class Location
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

    /**
     * @var integer
     *
     * @ORM\Column(name="cap", type="integer", nullable=false)
     */
    private $cap;

    /**
     * @var float
     *
     * @ORM\Column(name="lat", type="float", precision=10, scale=6, nullable=false)
     */
    private $lat;

    /**
     * @var float
     *
     * @ORM\Column(name="lng", type="float", precision=10, scale=6, nullable=false)
     */
    private $lng;

    /**
     * @var string
     *
     * @ORM\Column(name="address", type="string", length=45, nullable=false)
     */
    private $address;

    /**
     * @var string
     *
     * @ORM\Column(name="city", type="string", length=45, nullable=false)
     */
    private $city;

    /**
     * @var \AppBundle\Entity\ShopUser
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\ShopUser")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="shop_id", referencedColumnName="id")
     * })
     * @Assert\Type(type="AppBundle\Entity\ShopUser")
     * @Assert\Valid()
     *
     *
     */
    private $shop;

    /**
     * @param ShopUser $shop
     */
    public function setShop(ShopUser $shop)
    {
        $this->shop = $shop;
    }


    /**
     * @return ShopUser
     */
    public function getShop()
    {
        return $this->shop;
    }

控制器/:

RegistrationShopUserController.php

<?php


namespace AppBundle\Controller;


use AppBundle\Entity\Branch;
use AppBundle\Entity\ShopUser;
use AppBundle\Form\BranchFormType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\BrowserKit\Request;

class RegistrationShopUserController extends Controller
{
    public function registerAction(){


        return $this->container
            ->get('pugx_multi_user.registration_manager')
            ->register(ShopUser::class);


    }
}

我认为问题出在 RegistrationShopUserController 中,但我不知道如何解决。请帮忙。

问题是 Location 实体和 ShopUser 实体之间存在单向一对多关系。当您在控制器中创建 shopUser 对象时,只是 "doesn't know" 应该与 Location.

有某种关系

解决方案是,您必须在 ShopUser 实体 class 中定义一个 属性,它将包含与 Location 实体 class 的关系(多对一是恕我直言,你想要什么)。 还要考虑如何在创建新的 shopUser 对象时处理 属性 - 如果您想在每次新用户注册时创建一个新位置,那么只需在 [=11= 中创建并分配一个新位置对象即可]实体class构造函数。

(如果您想在新用户注册期间创建新位置并从现有位置 select,则需要更复杂的解决方案。)

我解决了这个问题!

在RegistrationShopUserFormType.php

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('lastName')
            ->add('shopName')
            ->add('mainLocation', LocationFormType::class);


        ;

    }

LocationFormType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('address')
            ->add('city')
            ->add('cap')
        ;

}

我在ShopUser.php

中添加了
/**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Location", mappedBy="shop", cascade={"persist"})
     */
    protected $locations;

    public function __construct()
    {
        $this->locationss = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getLocations()
    {
        return $this->locations;
    }


    public function getMainLocation()
    {
        return $this->locations->first() ? $this->locations->first() : null;
    }

    public function setMainLocation(Location $location)
    {
        if ($this->locations->contains($location)) {
            return;
        }

        $this->locations[] = $location;
        $branch->setShop($this);
    }

有效!希望有用。