当我创建一个映射了2个实体的表单时,我收到错误

时间:2015-09-01 09:53:23

标签: forms symfony

我有2个实体,Topic.php和Post.php我想有这个:

enter image description here

TopicType.php:

<?php

namespace BISSAP\ForumBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TopicType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('title', 'text', array(
                    'label' => 'Titre'))
                ->add('subForm', new PostType())
                ->add('save', 'submit', array(
                    'attr' => array(
                    'class' => 'btn right-flt')))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BISSAP\ForumBundle\Entity\Topic'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'bissap_forumbundle_topic';
    }
}

PostType.php:

<?php

namespace BISSAP\ForumBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PostType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('content', 'ckeditor', array(
                    'label' => 'Votre message',
                    'config_name' => 'my_custom_config',
                    'config' => array('language' => 'fr')))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BISSAP\ForumBundle\Entity\Post'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'bissap_forumbundle_post';
    }
}

Topic.php:

<?php

namespace BISSAP\ForumBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/*use BISSAP\BodyConcept\Entity\Forum;
*/

/**
 * Topic
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\TopicRepository")
 */
class Topic
{
    /**
     * @ORM\ManyToOne(targetEntity="Forum", inversedBy="Topics", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $forum;

    /**
     * @var ArrayCollection $Posts
     *
     * @ORM\OneToMany(targetEntity="Post", mappedBy="Topic", cascade={"persist", "remove", "merge"})
     */
    private $posts;

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

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="view_count", type="integer")
     */
    private $viewCount;

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

    /**
     * @var integer
     *
     * @ORM\Column(name="reply_count", type="integer")
     */
    private $replyCount;

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

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

    /**
     * @var integer
     *
     * @ORM\Column(name="last_post", type="integer")
     */
    private $lastPost;

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


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

    /**
     * Set title
     *
     * @param string $title
     * @return Topic
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set user
     *
     * @param integer $user
     * @return Topic
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return integer 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set viewCount
     *
     * @param integer $viewCount
     * @return Topic
     */
    public function setViewCount($viewCount)
    {
        $this->viewCount = $viewCount;

        return $this;
    }

    /**
     * Get viewCount
     *
     * @return integer 
     */
    public function getViewCount()
    {
        return $this->viewCount;
    }

    /**
     * Set dateCreation
     *
     * @param \DateTime $dateCreation
     * @return Topic
     */
    public function setDateCreation($dateCreation)
    {
        $this->dateCreation = $dateCreation;

        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return \DateTime 
     */
    public function getDateCreation()
    {
        return $this->dateCreation;
    }

    /**
     * Set replyCount
     *
     * @param integer $replyCount
     * @return Topic
     */
    public function setReplyCount($replyCount)
    {
        $this->replyCount = $replyCount;

        return $this;
    }

    /**
     * Get replyCount
     *
     * @return integer 
     */
    public function getReplyCount()
    {
        return $this->replyCount;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Topic
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * Set genre
     *
     * @param string $genre
     * @return Topic
     */
    public function setGenre($genre)
    {
        $this->genre = $genre;

        return $this;
    }

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

    /**
     * Set lastPost
     *
     * @param integer $lastPost
     * @return Topic
     */
    public function setLastPost($lastPost)
    {
        $this->lastPost = $lastPost;

        return $this;
    }

    /**
     * Get lastPost
     *
     * @return integer 
     */
    public function getLastPost()
    {
        return $this->lastPost;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Topic
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

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

    /**
     * Set forum
     *
     * @param Forum $forum
     * @return Topic
     */
    /*public function setForum(\BISSAP\BodyConceptBundle\Entity\Forum $forum)*/
    public function setForum(Forum $forum)
    {
        $this->forum = $forum;

        return $this;
    }

    /**
     * Get forum
     *
     * @return \BISSAP\BodyConceptBundle\Entity\Forum 
     */
    public function getForum()
    {
        return $this->forum;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
    $this->dateCreation = new \DateTime();
    }

    /**
     * Add posts
     *
     * @param \BISSAP\ForumBundle\Entity\Post $posts
     * @return Topic
     */
    public function addPost(\BISSAP\ForumBundle\Entity\Post $posts)
    {
        $this->posts[] = $posts;

        return $this;
    }

    /**
     * Remove posts
     *
     * @param \BISSAP\ForumBundle\Entity\Post $posts
     */
    public function removePost(\BISSAP\ForumBundle\Entity\Post $posts)
    {
        $this->posts->removeElement($posts);
    }

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

Post.php:

<?php

namespace BISSAP\ForumBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Post
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\PostRepository")
 */
class Post
{

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

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

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

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

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

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

    /**
     * @ORM\ManyToOne(targetEntity="Topic", inversedBy="Posts", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false)
     */
    private $topic;



    /**
     * Constructor
     */
    public function __construct()
    {
    $this->dateCreation = new \DateTime();
    $this->dateModif = new \DateTime();
    }

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

    /**
     * Set content
     *
     * @param string $content
     * @return Post
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

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

    /**
     * Set dateCreation
     *
     * @param \DateTime $dateCreation
     * @return Post
     */
    public function setDateCreation($dateCreation)
    {
        $this->dateCreation = $dateCreation;

        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return \DateTime 
     */
    public function getDateCreation()
    {
        return $this->dateCreation;
    }

    /**
     * Set dateModif
     *
     * @param \DateTime $dateModif
     * @return Post
     */
    public function setDateModif($dateModif)
    {
        $this->dateModif = $dateModif;

        return $this;
    }

    /**
     * Get dateModif
     *
     * @return \DateTime 
     */
    public function getDateModif()
    {
        return $this->dateModif;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Post
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

    /**
     * Set Topic
     *
     * @param Topic $topic
     * @return Post
     */
    /*public function setTopic(\BISSAP\ForumBundle\Entity\Topic $topic)*/
    public function setTopic(Topic $topic)
    {
        $this->topic = $topic;

        return $this;
    }

    /**
     * Get Topic
     *
     * @return \BISSAP\ForumBundle\Entity\Topic 
     */
    public function getTopic()
    {
        return $this->topic;
    }

    /**
     * Set user
     *
     * @param \BISSAP\ForumBundle\Entity\User $user
     * @return Post
     */
    public function setUser(\BISSAP\UserBundle\Entity\User $user)
    {
        $this->user = $user;

        return $this;
    }

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

这是我的控制器的一部分,在这里我打电话并使用表格:

public function categoryAction(Request $request)
    {

    $em = $this->getDoctrine()->getManager();

        $topic = new \BISSAP\ForumBundle\Entity\Topic();
        $form = $this->createForm(new TopicType(), $topic);

        $user = $this->getUser();
        $forum = $em->getRepository('BISSAPForumBundle:Forum')->find(1);

        if ($form->handleRequest($request)->isValid()) {

            $topic->setUser($user);
            $topic->setForum($forum);
            $topic->setViewCount(23);
            $topic->setReplyCount(123);
            $topic->setLastPost(25);
            $topic->setSlug('slug_sluggg');
            $topic->setGenre('genre');
            $em->persist($topic);
            $em->flush();


            return $this->render('BISSAPForumBundle:F:topic-forum.html.twig', array('user' => $user, 'topic' => $topic));
        }
            return $this->render('BISSAPForumBundle:F:category-forum.html.twig', array('listTopics' => $listTopics, 'catId' => $catId, 'form' => $form->createView(), 'user' => $user));

    }

我有这个错误:属性&#34; subForm&#34;也不是其中一种方法&#34; getSubForm()&#34;,&#34; isSubForm()&#34;,&#34; hasSubForm()&#34;,&#34; __ get()&# 34;在课堂上存在且具有公共访问权限&#34; BISSAP \ ForumBundle \ Entity \ Topic&#34;。

苏尔的方式是错的,因为我认为,我需要给出&#34; $ topic objexct&#34;和&#34; $ post object&#34;我用的时候: $ form = $ this-&gt; createForm(new TopicType(),$ topic);

我尝试过,添加(收藏)我得到类似的错误!

谢谢你。

1 个答案:

答案 0 :(得分:1)

您得到的错误是因为您的主题类中没有“子表单”属性。该名称应与主题类中属性的名称相对应。

所以这个:

->add('subForm', new PostType())

应改为

$builder->add('posts','collection', array(  'type' => new PostType()))

http://jmeter.512774.n5.nabble.com/automatic-correlation-for-jmeter-td5531399.html会有所帮助。