[英]Symfony2 forms collection
我有兩個實體,Topic和TopicContent由topic_id建立關系。
主題表中的topic_id是autoincremet,在topic_content中不是自動增量。
因為,查詢正常時,我需要從Topic實體獲取topic_id,然后將數據插入topic_content表中。 請幫助我,如何與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(),
));
}
保留主題實體后,您將從主題實體獲取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
實體保持插入新行的對象。
$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”負擔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.