【问题标题】:Entities relation, form builder实体关系,表单构建器
【发布时间】:2014-03-18 00:21:07
【问题描述】:

我有两个实体主题 和主题内容

当我从主题实体发送数据时,一切正常。但是当我将 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());

典型类型

$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());

主题内容类型

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"]

主题内容

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"]

【问题讨论】:

    标签: symfony doctrine many-to-many


    【解决方案1】:

    您在 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())));
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-19
    • 1970-01-01
    • 2019-12-25
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多