我有两个实体主题 和TopicContent
当我从Topic实体发送数据时,一切正常。但是当我将TopicContentType包含在我的表单构建器中时,有一个错误..朋友,请帮帮我..
我的控制器
public function createAction(Request $request)
{
$entity = new Topic();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('application_club_show', array('id' => $entity->getTopicId())));
}
return $this->render('ApplicationClubBundle:Topic:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
$builder
->add('user_id', 'hidden')
->add('topic_title')
->add('topic_tags')
->add('topic_publish', 'hidden')
->add('topic_user_ip', 'hidden')
->add('topic_count_read', 'hidden')
->add('topic_count_comment', 'hidden')
;
$builder->add('topic_content', new \Application\ClubBundle\Form\TopicContentType());
TypicType
$builder
->add('user_id', 'hidden')
->add('topic_title')
->add('topic_tags')
->add('topic_publish', 'hidden')
->add('topic_user_ip', 'hidden')
->add('topic_count_read', 'hidden')
->add('topic_count_comment', 'hidden')
;
$builder->add('topic_content', new \Application\ClubBundle\Form\TopicContentType());
TopicContentType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('topic_id')
->add('topic_text')
;
}
主题内容实体
<?php
namespace Application\ClubBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class TopicContent
{
/**
*
* @var integer
*/
private $topic_id;
/**
* @var string
*/
private $topic_text;
/**
* @var \Application\ClubBundle\Entity\Topic
*/
private $topic;
/**
* Set topic_id
*
* @param integer $topicId
* @return TopicContent
*/
public function setTopicId($topicId)
{
$this->topic_id = $topicId;
return $this;
}
/**
* Get topic_id
*
* @return integer
*/
public function getTopicId()
{
return $this->topic_id;
}
/**
* Set topic_text
*
* @param string $topicText
* @return TopicContent
*/
public function setTopicText($topicText)
{
$this->topic_text = $topicText;
return $this;
}
/**
* Get topic_text
*
* @return string
*/
public function getTopicText()
{
return $this->topic_text;
}
/**
* Set topic
*
* @param \Application\ClubBundle\Entity\Topic $topic
* @return TopicContent
*/
public function setTopic(\Application\ClubBundle\Entity\Topic $topic = null)
{
$this->topic = $topic;
return $this;
}
/**
* Get topic
*
* @return \Application\ClubBundle\Entity\Topic
*/
public function getTopic()
{
return $this->topic;
}
}
主题实体
/**
* Add topic_content
*
* @param \Application\ClubBundle\Entity\TopicContent $topicContent
* @return Topic
*/
public function setTopicContent(\Application\ClubBundle\Entity\TopicContent $topicContent)
{
$this->topic_content[] = $topicContent;
return $this;
}
我的实体相关 主题
Application\ClubBundle\Entity\Topic:
type: entity
table: topic
id:
topic_id:
type: integer
generator: { strategy: AUTO }
fields:
topic_title:
type: string
length: 200
oneToMany:
topic_content:
targetEntity: TopicContent
mappedBy: topic
topic_tag:
targetEntity: TopicTag
mappedBy: topic
cascade: ["persist"]
TopicContent
Application\ClubBundle\Entity\TopicContent:
type: entity
table: topic_content
id:
topic_id:
type: integer
fields:
topic_text:
type: text
oneToOne:
topic:
targetEntity: Topic
inversedBy: topic_content
joinColumn:
name: topic_id
referencedColumnName: topic_id
cascade: ["persist"]
答案 0 :(得分:0)
您可以在Symfony显示的错误消息中得到答案:您需要保留Topic
实体和TopicContent
实体。为此,您必须在关系上定义cascade={"persist"}
。
修改强>
另一种持久化TopicContent
的方法是在表单处理函数中明确地执行它:
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$content = $entity->getTopicContent();
$em->persist($entity);
$em->persist($content);
$em->flush();
return $this->redirect($this->generateUrl('application_club_show', array('id' => $entity->getTopicId())));
}