php - How to get last inserted id in zf2? -
i new in zf2. make create action , want last inserted id calender_id equal zero. how can last insert id in create action ?
here code:
public function createaction() { if ($this->zfcuserauthentication()->hasidentity()) { $form = new calendarform(); $form->get('user_id')->setvalue($this->zfcuserauthentication()->getidentity()->getid()); $form->get('submit')->setvalue('add'); $request = $this->getrequest(); if ($request->ispost()) { $calendar = new calendar(); $form->setinputfilter($calendar->getinputfilter()); $form->setdata($request->getpost()); if ($form->isvalid()) { $calendar->exchangearray($form->getdata()); $this->getcalendartable()->savecalendar($calendar); if($request->isxmlhttprequest()) { //$dm = $this->getservicelocator()->get('doctrine.documentmanager.odm_default'); //$calender = new \calendar\model\calendar($dm); $response = new response(); $calender_id = $calendar->calendar_id; $userid =$calendar->user_id; $title=$calendar->title; $description=$calendar->description; $output = array('success' => true, 'calendar_id' => $calender_id, 'user_id' => $userid, 'title' => $title, 'description' => $description); //$response->headers->set('content-type', 'application/json'); $response->setcontent(json_encode($output)); return $response; } return $this->redirect()->toroute('calendar'); } } return array('form' => $form); } else { $this->redirect()->toroute('zfcuser/login'); } }
how last inserted id?
if calendartable extends tablegateway can use $calendartable->getlastinsertvalue()
last insert id. can use method in savecalendar
method.
Comments
Post a Comment