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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动