Symfony2形成集合

时间:2014-03-20 07:34:51

标签: php symfony orm doctrine-orm

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

主题表中的

topic_id是autoincremet,在topic_content中它不是自动增量。

因为,我需要在查询时从Topic实体获取topic_id,然后在topic_content表中插入数据。请帮助我,如何在与orm的symfony中这样做。感谢。

TopicContent     

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;
    }
}

enter image description here

主题实体

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

1 个答案:

答案 0 :(得分:0)

在保留主题实体后,您将从Topic实体获取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'负担