douwen5584 2014-04-22 09:10
浏览 15
已采纳

我如何使用带有Doctrine 2 ODM的mongoDB在zf2中保存数据?

i make a controller in zf2 for save data in mongodb but it not save any record in event table,how i save data?here is my code:

 public function createAction()
    {
            $calendar_id = (int) $this->params()->fromRoute('id', 0);
            if ($calendar_id == 0) {
                return $this->redirect()->toRoute('calendar', array(
                    'action' => 'index'
                ));
            }

            //echo $calendar_id;
            $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
            $form = new EventForm();
            $update=false;
            $message='';

            $form->get('calendar_id')->setValue($id);
            $form->get('submit')->setValue('Add');

            if ($this->getRequest()->isPost()) {

                $post = $this->getRequest()->getPost();
                $form->setInputFilter($form->getInputFilter());
                $form->setData($post);

                if ($form->isValid()) {
                    $formData=$form->getData();
                    $s = new Event();
                    $s->setProperty('calendar_id',$calendar_id);
                    $s->setProperty('title',$post['title']);
                    $s->setProperty('description',$post['description']);
                    $s->setProperty('startdate',$post['begin']);
                    $s->setProperty('enddate',$post['end']);
                    $dm->persist($s);
                    $dm->flush();
                    $update=1;
                    $message='calendar Added Successfully.';

                    //$form = new CalendarForm();
                    //$this->redirect()->toRoute('calendar');
                }
            }
            return array('form' => $form, 'add_message' => $message, 'update' => $update, 'calendar'=>$this->calendar);
    }
  • 写回答

1条回答 默认 最新

  • dongzhuandian3292 2014-04-22 10:48
    关注

    I set code and save data using mongoodm,here is my code:

    public function createAction()
        {
            $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
                $calendar_id = (int) $this->params()->fromRoute('id', 0);
                if ($calendar_id == 0) {
                    return $this->redirect()->toRoute('calendar', array(
                        'action' => 'index'
                    ));
                }
    
    
                $form = new EventForm();
                $update=false;
                $message='';
    
                $form->get('calendar_id')->setValue($calendar_id);
                $form->get('submit')->setValue('Add');
    
                if ($this->getRequest()->isPost()) {
    
                    $post = $this->getRequest()->getPost();             
                    $form->setInputFilter($form->getInputFilter());             
                    $form->setData($post);      
    
                    if ($form->isValid()) {
    
                        $formData=$form->getData(); 
                        $s = new Event();
                        $s->setProperty('calendar_id',$post['calendar_id']);
                        $s->setProperty('title',$post['title']);
                        $s->setProperty('description',$post['description']);
                        $s->setProperty('startdate',$post['begin']);
                        $s->setProperty('enddate',$post['end']);
                        $dm->persist($s);
                        $dm->flush();
                        $update=1;
                        $message='calendar Added Successfully.';
    
                        $form = new EventForm();
                        $this->redirect()->toRoute('calendar');
                    }
                }
                return array('form' => $form, 'add_message' => $message, 'update' => $update, 'calendar'=>$this->calendar);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献