Symfony 2.8 FOSUserBundle +嵌入formtype

时间:2016-02-17 17:05:39

标签: php symfony fosuserbundle pugxmultiuserbundle

我目前正在使用FOSUserBundle和PUGXmultiUserBundle处理symfony 2.8。

我尝试将注册表格与另一种形式合并,并与双向关系。

创建用户帐户时,必须沿实体关系的一行创建帐户。

错误是:

  

执行'INSERT INTO沙龙时发生异常(nom,   描述,电话,电子邮件,adresse,ville,CP,siret,date,user_id)   价值(?,?,?,?,?,?,?,?,?,?)'与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]:完整性约束违规:1048列'user_id'   不能为空

PUGXMultiUserBundle => UserOne实体:

    <?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实体:

<?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;
    }
}

和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';
    }

}

也许这里有人已经遇到过这个问题,欢迎提供一点帮助。

0 个答案:

没有答案