0

我使用PUGXMultiUserBundle,因为我有两种类型的用户:CustomerUser和ShopUser。 ShopUser与位置(地址,城市...)有一对一的关系。PUGXMultiUserBundle和注册表格嵌入

我需要将所有数据保存到仅来自RegistrationShopUserFormType的表格(商店和位置)。我如何使用PUGX做到这一点?

我试过使用表单嵌入和表单集合,但我没有一个好的结果。 你有什么想法吗?

EDIT

这里的代码:

窗体/:

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)-without 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 -without一些吸气剂和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中,但我不知道如何解决它。请帮助。

+0

显示你已经尝试了什么,什么是不工作。 –

+0

我更新了帖子@JasonRoman – Stek

+0

好的 - 你提到你没有得到好的结果。你能发布你得到的结果和你期望的结果吗? –

回答

0

我解决了这个问题!

在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); 
    } 
添加

它的工作原理!我希望这是有用的。

0

问题是您在Location实体和ShopUser实体之间存在单向一对多关系。当您在控制器中创建shopUser对象时,仅仅是“不知道”与Location应该有某种关系。

解决的办法是,你必须在ShopUser实体类中定义一个属性,该实体类将包含与Location实体类的关系(多对一是你想要的IMHO)。 还要考虑如何在创建新ShopUser对象时处理该属性 - 如果要在每次新用户注册时创建一个新位置,则只需在实体类构造函数中创建并分配一个新位置对象。

(将需要更复杂的解决方案,如果你需要创建新的位置,也是一个新的用户注册过程中从现有位置选择。)

+0

谢谢你的答案。但是我需要ShopUser和Location之间的OneToMany。 ShopUser有一个/多个位置。 我该如何解决它? @JanRydrych – Stek