当我发送表格时,我得到:
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
正常,因为我有几个实体之间的关系映射(Topic.php,Post.php和User.php)
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';
}
}
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('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));
}
所以,我希望在setTopic()
之前访问$ post对象以访问setUser()
和flush()
...我想使用$form->getData()
,但我不知道如何使用它来获取次要对象($ post)。
当我提交表单时,两个对象都是持久的,主题和帖子,抱歉,但我没有看到主题标题的问题..?!