2

I am currently working on a symfony 2.8 with FOSUserBundle and PUGXmultiUserBundle.

I try to incorporate the registration form with another form, with two-way relationship.

When you create a user account must be created along a line of the entity relationship.

The error is:

An exception occurred while executing 'INSERT INTO salon (nom, description, phone, email, adresse, ville, CP, siret, date, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["Le salon de la rue", "desc", "0223254578", "salon2larue@gmail.com", "2 rue de la motte", "Saint-Gr\u00e9goire", "35000", "456987887456", "2016-02-17 17:55:52", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

PUGXMultiUserBundle => UserOne entity :

    <?php

namespace UserBundle\Entity;

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


/**
 * @ORM\Entity
 * @ORM\Table(name="user_one")
 * @UniqueEntity(fields = "username", targetClass = "UserBundle\Entity\User", message="fos_user.username.already_used")
 * @UniqueEntity(fields = "email", targetClass = "UserBundle\Entity\User", message="fos_user.email.already_used")
 */
class UserOne extends User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
    * @ORM\OneToMany(targetEntity="CoreBundle\Entity\Salon", mappedBy="user", cascade={"persist","remove"})
    */
    private $salons;


    public function __construct()
    {
        parent::__construct();
        // your own logic
        $this->roles = array('ROLE_SALON');
        $this->salons = new ArrayCollection();
    }

    /**
     * Add salons
     *
     * @param \CoreBundle\Entity\Salon $salons
     * @return UserOne
     */
    public function addSalon(\CoreBundle\Entity\Salon $salons)
    {
        $this->salons[] = $salons;
        $salons->setUser($this);
        return $this;
    }

    /**
     * Remove salons
     *
     * @param \CoreBundle\Entity\Salon $salons
     */
    public function removeSalon(\CoreBundle\Entity\Salon $salons)
    {
        $this->salons->removeElement($salons);
    }

    /**
     * Get salons
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSalons()
    {
        return $this->salons;
    }
}

CoreBundle Salon entity :

<?php

namespace CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use UserBundle\Entity\UserOne;

/**
 * Salon
 *
 * @ORM\Table(name="salon")
 * @ORM\Entity(repositoryClass="CoreBundle\Repository\SalonRepository")
 */
class Salon
{

    /**
   * @ORM\ManyToOne(targetEntity="UserBundle\Entity\UserOne", inversedBy="salons", cascade={"persist"})
   * @ORM\JoinColumn(nullable=false)
   */
    private $user;

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

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

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

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

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

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

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

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

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

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    public function __construct()
    {
        $this->date = new \Datetime();
    }


    /**
     * Set user
     *
     * @param \UserBundle\Entity\UserOne $user
     * @return Salon
     */
    public function setUser(\UserBundle\Entity\UserOne $user)
    {
        $this->user = $user;

        return $this;
    }

      //Others Getters/Setters

    /**
     * Get user
     *
     * @return \UserBundle\Entity\UserOne 
     */
    public function getUser()
    {
        return $this->user;
    }
}

And RegistrationUserOneFormType :

<?php
namespace UserBundle\Form\Type;

use CoreBundle\Form\SalonType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

class RegistrationUserOneFormType  extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
        //->add('salons', new SalonType(), array('mapped' => false));
        /*->addEventListener(FormEvents::POST_SET_DATA, function(FormEvent $event) {
            $user = $event->getData();
        })*/
        ->add('salons', 'collection', [
                        'type' => new SalonType,
                        'allow_add' => true,
                        'allow_delete' => false
                ])
        ;
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

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

}

Maybe someone here has already been confronted with the problem, a little help would be welcome.

Frank Drebin
  • 71
  • 1
  • 8
  • Are you sur that your controller explicitely persists your user and each of his salons ? – Alsatian Feb 17 '16 at 17:54
  • No, i dont know. I should override/custom my RegistrationUserOneController but I do not know how to go with FOSUserBundle + PUGXMultiUserBundle – Frank Drebin Feb 17 '16 at 20:53
  • OK, I didn't notice you haven't access to the contoller. But I think the cascade={persist} should work. No other idea, sorry. – Alsatian Feb 19 '16 at 09:48

0 Answers0