集合表单返回数组而不是OneToMany中的对象

时间:2016-12-24 12:03:32

标签: symfony

我有一个问题,我有两个实体,Contenu(一个)和文章(很多),我已经建立了我的关系,它是双向的。

我使用FormType imbricate,当我提交表单时,我有一个数组用于我的实体文章而不是一个对象,我不能坚持使用数组。

如果您有任何解决方案,谢谢

实体Contenu

<?php

/**
 * Contenu
 *
 * @ORM\Table(name="ge_contenu")
 * @ORM\Entity(repositoryClass="GE\MainBundle\Repository\ContenuRepository")
 */
class Contenu
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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


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

    /**
     * @ORM\OneToMany(targetEntity="AdminBundle\Entity\Article", mappedBy="contenu")
     */
    private $articles;    


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

    /**
     * Set texte
     *
     * @param string $texte
     *
     * @return Contenu
     */
    public function setTexte($texte)
    {
        $this->texte = $texte;

        return $this;
    }

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

    /**
     * Set entete
     *
     * @param string $entete
     *
     * @return Contenu
     */
    public function setEntete($entete)
    {
        $this->entete = $entete;

        return $this;
    }

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

    /**
     * Set conclusion
     *
     * @param string $conclusion
     *
     * @return Contenu
     */
    public function setConclusion($conclusion)
    {
        $this->conclusion = $conclusion;

        return $this;
    }

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

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->articles = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add article
     *
     * @param \AdminBundle\Entity\Article $article
     *
     * @return Contenu
     */
    public function addArticle(\AdminBundle\Entity\Article $article)
    {
        $this->articles[] = $article;

        $article->setContenu($this);

        return $this;
    }

    /**
     * Remove article
     *
     * @param \AdminBundle\Entity\Article $article
     */
    public function removeArticle(\AdminBundle\Entity\Article $article)
    {
        $this->articles->removeElement($article);
    }

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

实体文章

    <?php

/**
 * Article
 *
 * @ORM\Table(name="ge_article")
 * @ORM\Entity(repositoryClass="GE\MainBundle\Repository\ArticleRepository")
 */
class Article
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
    * @var Article $article
    *
    * @ORM\ManyToOne(targetEntity="AdminBundle\Entity\Contenu", inversedBy="articles")
    * @ORM\JoinColumn(nullable=false)
    */
    public $contenu;

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

    /**
     * Set titre
     *
     * @param string $titre
     *
     * @return Article
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

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

    /**
     * Set texte
     *
     * @param string $texte
     *
     * @return Article
     */
    public function setTexte($texte)
    {
        $this->texte = $texte;

        return $this;
    }

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

    /**
     * Set contenu
     *
     * @param \AdminBundle\Entity\Contenu $contenu
     *
     * @return Article
     */
    public function setContenu(\AdminBundle\Entity\Contenu $contenu)
    {
        $this->contenu = $contenu;

        return $this;
    }

    /**
     * Get contenu
     *
     * @return \AdminBundle\Entity\Contenu
     */
    public function getContenu()
    {
        return $this->contenu;
    }
}

我的控制器

    public function contenuAction($id = null, Request $request)
{
    $em = $this
      ->getDoctrine()
      ->getManager()        
    ;

    $pages_contenu = $em->getRepository('AdminBundle:Page')->findBy(
        array("contenu" => true),
        array("nom" => "asc"),
        null,
        null
    );

    $vars = array(
        "modif" => null,
        "modif" => null,
        "pages_contenu" => $pages_contenu
    );


    if (isset($id)) 
    {
        $contenu = $em->getRepository('AdminBundle:Contenu')->findOneById($id);

        if ($contenu == null) 
        {
            $contenu = new Contenu();
            $vars["modif"] = $contenu;
        }
        else
        {
            $vars["modif"] = $contenu;
        }
    }
    else
    {
        $contenu = new Contenu();
    }

    $form = $this->get('form.factory')->create(ContenuType::class, $contenu);

    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) 
    {


        $em->persist($contenu);
        $em->flush();

        $request->getSession()->getFlashBag()->set('valid', 'Contenu bien enregistré.');

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

    $vars['form'] = $form->createView();

    return $this->render('AdminBundle:Admin:contenu.html.twig', $vars);
}

Contenu Type

    class ContenuType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
      ->add('entete',       TextareaType::class)
      ->add('texte',        TextareaType::class)
      ->add('articles',      CollectionType::class, array(
              'entry_type'   => ArticleType::class,
              'allow_add'    => true,
              'allow_delete' => true
            ))
      ->add('conclusion',   TextareaType::class)
      ->add('submit', SubmitType::class, array('label' => 'Enregistrer')) 
    ;
  }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
      'data_class' => 'AdminBundle\Entity\Contenu'
    ));
  }

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

Article Type

    class ArticleType extends AbstractType
{
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
      ->add('titre',        TextType::class)
      ->add('texte',        TextareaType::class)
    ;
  }

  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
      'data_class' => 'AdminBundle\Entity\Article'
    ));
  }

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

1 个答案:

答案 0 :(得分:0)

您必须将以下方法添加到ArticleType类:

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'AdminBundle\Entity\ArticleType'
    ));
}