无法保存一对多的数据

时间:2016-07-18 14:07:33

标签: php symfony doctrine

我创建了一个社会与员工之间的一对多关系。一切都很好,我可以在编辑社交页面时查看用户列表,我可以选择其中一些,但问题是我无法保存/保存这些数据,而且我不会#39不知道为什么。

我尝试了很多内容,例如在FormType中添加allow_add =>trueby_reference => false,但即使它仍然保存了表单的其余部分,例如社会和#39;名称及其地址。

以下是实体:

Societe.php

<?php

namespace SocieteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Societe
 *
 * @ORM\Table(name="societe")
 * @ORM\Entity(repositoryClass="SocieteBundle\Repository\SocieteRepository")
 */
class Societe
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @ORM\OneToMany(targetEntity="UserBundle\Entity\User", mappedBy="entreprise", cascade={"persist"})
     */
    private $salaries;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set raisonSociale
     *
     * @param string $raisonSociale
     *
     * @return Societe
     */
    public function setRaisonSociale($raisonSociale)
    {
        $this->raisonSociale = $raisonSociale;

        return $this;
    }

    /**
     * Get raisonSociale
     *
     * @return string
     */
    public function getRaisonSociale()
    {
        return $this->raisonSociale;
    }

    /**
     * Set adresse
     *
     * @param string $adresse
     *
     * @return Societe
     */
    public function setAdresse($adresse)
    {
        $this->adresse = $adresse;

        return $this;
    }

    /**
     * Get adresse
     *
     * @return string
     */
    public function getAdresse()
    {
        return $this->adresse;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->salaries = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add salary
     *
     * @param \UserBundle\Entity\User $salary
     *
     * @return Societe
     */
    public function addSalary(\UserBundle\Entity\User $salary)
    {
        $this->salaries[] = $salary;

        return $this;
    }

    /**
     * Remove salary
     *
     * @param \UserBundle\Entity\User $salary
     */
    public function removeSalary(\UserBundle\Entity\User $salary)
    {
        $this->salaries->removeElement($salary);
    }

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

    /**
     * Set salaries
     * Something I tried, didn't work
     */
    public function setSalaries($salaries)
    {
        $this->salaries = $salaries;

        return $this;
    }
}

user.php的

<?php

namespace UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="prenom", type="string", length=255, nullable=true)
     */
    protected $prenom;

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

    /**
     * @var string
     * @ORM\Column(name="telephone", type="string", length=255, nullable=true)
     */
    protected $telephone;

    /**
     * @ORM\OneToMany(targetEntity="MissionBundle\Entity\Mission", mappedBy="createur", cascade={"remove", "persist"})
     *
     */
    protected $post;

    /**
     * @ORM\ManyToOne(targetEntity="EcoleBundle\Entity\Ecole", inversedBy="representant", cascade={"persist"})
     * @ORM\JoinColumn(name="ecole_id", referencedColumnName="id")
     */
    protected $ecole;

    /**
     * @ORM\ManyToOne(targetEntity="SocieteBundle\Entity\Societe", inversedBy="salaries", cascade={"persist"})
     * @ORM\JoinColumn(name="salaries_id", referencedColumnName="id")
     */
    private $entreprise;

    /**
     * Set prenom
     *
     * @param string $prenom
     *
     * @return User
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;

        return $this;
    }

    /**
     * Get prenom
     *
     * @return string
     */
    public function getPrenom()
    {
        return $this->prenom;
    }

    /**
     * Set nom
     *
     * @param string $nom
     *
     * @return User
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set telephone
     *
     * @param string $telephone
     *
     * @return User
     */
    public function setTelephone($telephone)
    {
        $this->telephone = $telephone;

        return $this;
    }

    /**
     * Get telephone
     *
     * @return string
     */
    public function getTelephone()
    {
        return $this->telephone;
    }

    /**
     * Add post
     *
     * @param \MissionBundle\Entity\Mission $post
     *
     * @return User
     */
    public function addPost(\MissionBundle\Entity\Mission $post)
    {
        $this->post[] = $post;

        return $this;
    }

    /**
     * Remove post
     *
     * @param \MissionBundle\Entity\Mission $post
     */
    public function removePost(\MissionBundle\Entity\Mission $post)
    {
        $this->post->removeElement($post);
    }

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

    /**
     * Set post
     *
     * @param \Doctrine\Common\Collections\Collection $comments
     */

    public function setDeskComment(\Doctrine\Common\Collections\Collection $post){

        $this->post = $post;

    }


    /**
     * Set ecole
     *
     * @param \EcoleBundle\Entity\Ecole $ecole
     *
     * @return User
     */
    public function setEcole(\EcoleBundle\Entity\Ecole $ecole = null)
    {
        $this->ecole = $ecole;

        return $this;
    }

    /**
     * Get ecole
     *
     * @return \EcoleBundle\Entity\Ecole
     */
    public function getEcole()
    {
        return $this->ecole;
    }

    /**
     * Set entreprise
     *
     * @param \SocieteBundle\Entity\Societe $entreprise
     *
     * @return User
     */
    public function setEntreprise(\SocieteBundle\Entity\Societe $entreprise = null)
    {
        $this->entreprise = $entreprise;

        return $this;
    }

    /**
     * Get entreprise
     *
     * @return \SocieteBundle\Entity\Societe
     */
    public function getEntreprise()
    {
        return $this->entreprise;
    }
}

SocieteType.php

<?php

namespace SocieteBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
//use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SocieteType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('raisonSociale')
            ->add('adresse')
            ->add('salaries', null, array(
                    'by_reference' => false,
                )
            )
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'SocieteBundle\Entity\Societe'
        ));
    }
}

SocieteController.php

<?php

namespace SocieteBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use SocieteBundle\Entity\Societe;
use SocieteBundle\Form\SocieteType;

/**
 * Societe controller.
 *
 * @Route("/societe")
 */
class SocieteController extends Controller
{
    /**
     * Lists all Societe entities.
     *
     * @Route("/", name="societe_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $societes = $em->getRepository('SocieteBundle:Societe')->findAll();

        return $this->render('societe/index.html.twig', array(
            'societes' => $societes,
        ));
    }

    /**
     * Creates a new Societe entity.
     *
     * @Route("/new", name="societe_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $societe = new Societe();
        $form = $this->createForm('SocieteBundle\Form\SocieteType', $societe);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($societe);
            $em->flush();

            return $this->redirectToRoute('societe_show', array('id' => $societe->getId()));
        }

        return $this->render('societe/new.html.twig', array(
            'societe' => $societe,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Societe entity.
     *
     * @Route("/{id}", name="societe_show")
     * @Method("GET")
     */
    public function showAction(Societe $societe)
    {
        $deleteForm = $this->createDeleteForm($societe);

        return $this->render('societe/show.html.twig', array(
            'societe' => $societe,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing Societe entity.
     *
     * @Route("/{id}/edit", name="societe_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Societe $societe)
    {
        $deleteForm = $this->createDeleteForm($societe);
        $editForm = $this->createForm('SocieteBundle\Form\SocieteType', $societe);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($societe);
            $em->flush();

            return $this->redirectToRoute('societe_edit', array('id' => $societe->getId()));
        }

        return $this->render('societe/edit.html.twig', array(
            'societe' => $societe,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Societe entity.
     *
     * @Route("/{id}", name="societe_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Societe $societe)
    {
        $form = $this->createDeleteForm($societe);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($societe);
            $em->flush();
        }

        return $this->redirectToRoute('societe_index');
    }

    /**
     * Creates a form to delete a Societe entity.
     *
     * @param Societe $societe The Societe entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Societe $societe)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('societe_delete', array('id' => $societe->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}

请告诉我,如果您要我在问题中添加文件,我会对其进行编辑。提前谢谢

编辑:如果我转储表单内容,我可以看到我选择的用户数组

1 个答案:

答案 0 :(得分:0)

在您的SocieteType.php更改:

->add('salaries', null, array(
    'by_reference' => false,
  )

->add('salaries', 'entity', array(
    'class' => 'SocieteBundle\Entity\Societe',
    'property' => 'raisonSociale',
  )