symfonyfosuserbundlesymfony-formsformcollectionpugxmultiuserbundle

PUGXMultiUserBundle and Registration form embedded


I use PUGXMultiUserBundle because I have two type of user: CustomerUser and ShopUser. ShopUser has a OneToMany relationship to Location(address,city...).

I need to save all data to tables (shop and location) from only RegistrationShopUserFormType. How can i do this with PUGX?

I've tried to use Form embedded and form collection, but I hasn't a good result. Have you got any idea?

EDIT

Here the code:

Form/:

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,
        ));
    }


}

Entity/: ShopUser.php (from PUGMultiUserBundle) -without getter and 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 some getter and 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;
    }

Controller/:

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);


    }
}

In my opinion the problem is in the RegistrationShopUserController, but I don't know how fix it. Help, please.


Solution

  • I solved this issue!

    In the 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')
            ;
    
    }
    

    I added in 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);
        }
    

    It works! I hope it's useful.