Question

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);
    }
Was it helpful?

Solution

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top