Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用集合symfony提交表单时发生异常_Php_Forms_Symfony - Fatal编程技术网

Php 使用集合symfony提交表单时发生异常

Php 使用集合symfony提交表单时发生异常,php,forms,symfony,Php,Forms,Symfony,当我发送表格时,我得到: An exception occurred while executing 'INSERT INTO Post (content, date_creation, date_modif, user_id, topic_id) VALUES (?, ?, ?, ?, ?)' with params ["<p>AA\/BB\/CC<\/p>", "2015-09-01 17:11:28", "2015-09-01 17:11:28", null, nu

当我发送表格时,我得到:

An exception occurred while executing 'INSERT INTO Post (content, date_creation, date_modif, user_id, topic_id) VALUES (?, ?, ?, ?, ?)' with params ["<p>AA\/BB\/CC<\/p>", "2015-09-01 17:11:28", "2015-09-01 17:11:28", null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
所以,我想在flush之前访问$post对象以访问setTopic和setUser…我想使用$form->getData,但我不知道如何使用它来获取辅助对象$post


当我提交表单时,两个对象被持久化,主题和帖子,很抱歉,但我看不出主题标题有问题

创建主题时,为什么要收集帖子,用户创建新主题时不应该只有一篇帖子?使用此配置,显示帖子的“我的视图”代码更轻。另外,我需要使用两个视图来显示帖子。我刚结束表单时的第一个视图和用户单击主题时的第二个视图;或者我可以做一个视图,但会有很多条件和循环…我不明白,所以任何用户都可以编辑任何帖子和主题标题?我还没有开始为主题和帖子编写编辑操作…你可能是对的…但我对我的项目没有太大的视野…我需要思考更多。再见
<?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';
    }
}
<?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('posts','collection', array(  'type' => new PostType(), 'label' => false, 'allow_add' => true,))
                ->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';
    }
}
public function categoryAction(Request $request)
{
    //Arguments recovery from url
    $URL = $request->getPathInfo();
    $element_URL = explode("/", $URL);
    $catId = $element_URL[3];
    (isset ($element_URL[4]))?$pageId = $element_URL[4]:$pageId = '1';


    $em = $this->getDoctrine()->getManager();
    //Pagination
    $findListTopics = $em->getRepository('BISSAPForumBundle:Topic')->findByForum($catId);
    $listTopics = $this->get('knp_paginator')
            ->paginate($findListTopics,$pageId/*page number*/,8/*limite per page*/)


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

    $user = $this->getUser();
    $forum = $em->getRepository('BISSAPForumBundle:Forum')->find(1);
    print_r($form->getData());
    if ($form->handleRequest($request)->isValid()) {

        $topic->setUser($user);
        $topic->setForum($forum);
        $topic->setViewCount(23);
        $topic->setContent("content a sup");
        $topic->setReplyCount(123);
        $topic->setLastPost(25);
        $topic->setSlug('slug_sluggg');
        $topic->setGenre('genre');

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

        $request->getSession()->getFlashBag()->add('notice', 'Sujet bien enregistrée.');

        return $this->redirect($this->generateUrl('bissap_forum_topic', array('topic_id' => $topic->getId())));
        //return $this->render('BISSAPForumBundle:F:topic-forum.html.twig', array('topicId' => $topic->getId(), 'user' => $user, 'topic' => $topic, 'firstDisplay' => true));
    }
        return $this->render('BISSAPForumBundle:F:category-forum.html.twig', array('listTopics' => $listTopics, 'catId' => $catId, 'form' => $form->createView(), 'user' => $user));


}