php - Symfony2 forms collection -
i have, 2 entities, topic , topiccontent relation topic_id.
topic_id in topic table autoincremet , in topic_content it's not autoincrement.
because, need topic_id topic entity when query it's ok, , insert data in topic_content table. me please, how in symfony orm. thanks.
topiccontent
namespace socialist\clubbundle\entity; use doctrine\orm\mapping 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; } /** * 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; } /** * 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; } /** * topic * * @return \socialist\clubbundle\entity\topic */ public function gettopic() { return $this->topic; } }
topic
namespace socialist\clubbundle\entity; use doctrine\orm\mapping 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; /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * topiccontent * * @return \socialist\clubbundle\entity\topiccontent */ public function gettopiccontent() { return $this->topiccontent; } }
topic entity
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; }
topic controller
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(), )); }
after persisting topic entity, lastinserted topic id topic entity example,
$em = $this->getdoctrine()->getmanager(); $topic = new \acme\testbundle\entity\topic(); $topic->settopictitle('your title'); $em->persist($topic); $em->flush(); //insert data table
if insertion ok $topic
entity hold new row object inserted can new topic id getting
$newtopicid= $topic->gettopicid();
for topic content entity,
$topiccontent= new new \acme\testbundle\entity\topiccontent(); $topiccontent->settopicid($newtopicid); $topiccontent->settopictext('your topic text');
for avoid more confusion please change topic content entity follows,
namespace socialist\clubbundle\entity; use doctrine\orm\mapping 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; /** * 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; } /** * 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; } /** * topic * * @return \socialist\clubbundle\entity\topic */ public function gettopic() { return $this->topic; } }`
the id
should primary key , autoincrement resolve 'topic_id' burdens
Comments
Post a Comment