简体   繁体   English

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

[英]how i save data in zf2 using mongoDB with Doctrine 2 ODM?

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: 我在zf2中创建了一个控制器,用于在mongodb中保存数据,但未在事件表中保存任何记录,我该如何保存数据?这是我的代码:

 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);
    }

I set code and save data using mongoodm,here is my code: 我使用mongoodm设置代码并保存数据,这是我的代码:

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM