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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。