Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用mongoDB和Doctrine 2 ODM在zf2中保存数据?_Php_Mongodb_Zend Framework2_Doctrine Odm - Fatal编程技术网

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

Php 如何使用mongoDB和Doctrine 2 ODM在zf2中保存数据?,php,mongodb,zend-framework2,doctrine-odm,Php,Mongodb,Zend Framework2,Doctrine Odm,我在zf2中制作了一个控制器,用于在mongodb中保存数据,但它没有在事件表中保存任何记录,如何保存数据?下面是我的代码: public function createAction() { $calendar_id = (int) $this->params()->fromRoute('id', 0); if ($calendar_id == 0) { return $this->red

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

我使用mongoodm设置代码并保存数据,以下是我的代码:

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

我不确定你是否理解你在这里做什么-你不应该通过请求实例访问未过滤的数据,因为它会绕过你的输入过滤器!这将取决于您的水合策略,但是
$formData
将保存过滤后的数据。发布
var\u dump($formData)