0

私はPUGXMultiUserBundleを使用します。これは2つのタイプのユーザー、CustomerUserとShopUserがあるためです。 ShopUserは、ロケーション(住所、都市など)とOneToManyの関係を持っています。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, 
     )); 
    } 


} 

エンティティ/:(PUGMultiUserBundleから) ShopUser.php -withoutゲッターとsetter-一部ゲッター-without

<?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と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エンティティの間に一方向の1対多の関係があることです。コントローラーにshopUserオブジェクトを作成するときは、Locationと何らかの関係があることを単に「知らない」だけです。

解決策は、Locationエンティティクラスとの関係を含むShopUserエンティティクラスのプロパティを定義する必要があるということです(多対1は必要なものです)。 新しいshopUserオブジェクトが作成されたときにそのプロパティを処理する方法について - 新しいユーザーが登録するたびに新しい場所を作成する場合は、ShopUserエンティティクラスコンストラクタで新しい場所オブジェクトを作成して割り当てます。

(新しい場所を作成しても、新規ユーザ登録時に既存の場所から選択することをお勧めします場合は、より複雑なソリューションが必要とされるであろう。)

+0

答えをありがとう。しかし、ShopUserとLocationの間にOneToManyが必要です。 ShopUserには1つ以上の場所があります。 どうすれば解決できますか? @JanRydrych – Stek

関連する問題