【问题标题】:Symfony2 forms collectionSymfony2 表单集合
【发布时间】:2014-03-20 07:34:51
【问题描述】:

我有两个实体,Topic 和 TopicContent 是 topic_id 的关系。

topic 表中的 topic_id 是 autoincremet ,而 topic_content 中的不是 autoincrement 。

因为查询没问题的时候需要从Topic实体中获取topic_id,然后在topic_content表中插入数据。请帮助我,如何在 symfony 中使用 orm 执行此操作。谢谢。

主题内容

namespace Socialist\ClubBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="topic_content")
 */
class TopicContent
{
    /**
     *
     * @ORM\Column(name="topic_id", type="integer")
     * @ORM\Id
     *
     */
    protected $topic_id;


    /**
     *
     * @ORM\Column(name="topic_text", type="text", nullable=false)
     */
    protected $topic_text;

    /**
     * @ORM\OneToOne(targetEntity="Topic", inversedBy="topicContent", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="topic_id", referencedColumnName="topic_id", onDelete="CASCADE")
     */
    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 \Socialist\ClubBundle\Entity\Topic $topic
     * @return TopicContent
     */
    public function setTopic(\Socialist\ClubBundle\Entity\Topic $topic = null)
    {
        $this->topic = $topic;

        return $this;
    }

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

主题

namespace Socialist\ClubBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="topic")
 * @ORM\HasLifecycleCallbacks
 */
class Topic
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $topic_id;

    /**
     * @ORM\Column(type="string", length=200)
     */
    protected $topic_title;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $topic_date_add;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $topic_date_edit;

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

    /**
     * Set topic_title
     *
     * @param string $topicTitle
     * @return Topic
     */
    public function setTopicTitle($topicTitle)
    {
        $this->topic_title = $topicTitle;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * @ORM\PrePersist
     */
    public function setTopicDateAddValue() {
        if (!$this->getTopicDateAdd())
        {
            $this->topic_date_add = new \DateTime();
        }
    }

    /**
     * @ORM\PreUpdate
     */
    public function setTopicDateEditValue()
    {
        $this->topic_date_edit = new \DateTime();
    }

    public function __construct()
    {
        $this->setTopicDateAdd(new \DateTime());
        $this->setTopicDateEdit(new \DateTime());
    }

    /**
     * @ORM\OneToOne(targetEntity="TopicContent", mappedBy="topic", cascade={"persist", "remove"})
     */
    private   $topicContent;

    /**
     * Set topicContent
     *
     * @param \Socialist\ClubBundle\Entity\TopicContent $topicContent
     * @return Topic
     */
    public function setTopicContent(\Socialist\ClubBundle\Entity\TopicContent $topicContent = null)
    {
        $this->topicContent = $topicContent;

        return $this;
    }

    /**
     * Get topicContent
     *
     * @return \Socialist\ClubBundle\Entity\TopicContent 
     */
    public function getTopicContent()
    {
        return $this->topicContent;
    }
}

主题实体

    public function __construct()
{
    $this->topicContent = new \Doctrine\Common\Collections\ArrayCollection();
    $this->setTopicDateAdd(new \DateTime());
    $this->setTopicDateEdit(new \DateTime());
}

 /**
 * Set topicContent
 *
 * @param \Socialist\ClubBundle\Entity\TopicContent $topicContent
 * @return Topic
 */
public function setTopicContent(\Socialist\ClubBundle\Entity\TopicContent $topicContent = null)
{
    $this->topicContent = $topicContent;

    return $this;
}

主题控制器

    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('topic_show', array('id' => $entity->getTopicId())));
    }

    return $this->render('SocialistClubBundle:Topic:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

【问题讨论】:

  • 首先,您确定需要一对一的关系吗?在你的情况下,一个主题得到一个内容?
  • 是的,我的朋友,我需要一对一的关系!我已经更新了我的帖子,请看:)
  • 问题不是来自你的实体(也不是教义)。它似乎工作。当您在控制器中创建主题时,您应该 setTopicContent()
  • 是的,你是对的!但是我如何在 TopicContent 实体中设置 id?我已经更新了帖子,..看看请:)
  • 请看我发布的答案,我想这就是你所期望的

标签: php symfony orm doctrine-orm


【解决方案1】:

持久化您的主题实体后,您将从您的主题实体获得 LastInserted 主题 ID 例如,

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

   $topic = new \Acme\TestBundle\Entity\Topic();
   $topic->setTopicTitle('Your Title');
   $em->persist($topic);
   $em->flush(); //insert data into table 

如果插入没问题$topic实体持有新的行对象插入 您可以通过获取来获取新的主题ID

 $newTopicId= $topic->getTopicId();

对于主题内容实体,

$topicContent= new new \Acme\TestBundle\Entity\TopicContent();
$topicContent->setTopicId($newTopicId);
$topicContent->setTopicText('Your topic text');

为避免更多混淆,请按如下方式更改您的主题内容实体,

namespace Socialist\ClubBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="topic_content")
 */
class TopicContent
{
    /**
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     *
     */
    protected $id;


    /**
     *
     * @ORM\Column(name="topic_text", type="text", nullable=false)
     */
    protected $topic_text;

    /**
     * @ORM\OneToOne(targetEntity="Topic", inversedBy="topicContent", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="topic_id", referencedColumnName="topic_id", onDelete="CASCADE")
     */
    private $topic;



    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->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 \Socialist\ClubBundle\Entity\Topic $topic
     * @return TopicContent
     */
    public function setTopic(\Socialist\ClubBundle\Entity\Topic $topic = null)
    {
        $this->topic = $topic;

        return $this;
    }

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

id 应该是主键和自动增量 这将解决您所有的“topic_id”负担

【讨论】:

  • 看看我的帖子,我已经添加了我的主题控制器的代码。有什么想法吗?
  • 从你的数据库设计来看,我认为 topic_content 实体中不需要 topic_id,因为你已经在主题内容中设置了与 $topic 的关系
  • 否则请在topic_content中设置与topic_id的关系会解决问题
  • > 请在topic_content中设置与topic_id的关系????这个怎么做??我认为我现在在我的实体中拥有?
  • 现在,一个问题是当我创建一个新主题时如何从数据库中获取自动增量 ID...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多