【发布时间】: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