0

i make a setting action in which i used doctrine query for getting single result but it returns null while data exist in database,how i get single result?here is my code:

public function settingsAction()
    {
            $id = (int) $this->params()->fromRoute('id', 0);
            if (!$id) {
                return $this->redirect()->toRoute('calendar', array(
                    'action' => 'create'
                ));
            }   
                 //echo $id;
            //$calendar = $id;//$this->getCalendarTable()->getCalendar($id);
            $calendar = $documentManager->getRepository('Calendar\Document\Calendar')->find($id);
            $form  = new CalendarForm();
            $form->bind($calendar);
            $form->get('submit')->setAttribute('value', 'Save');

            $request = $this->getRequest();
            if ($request->isPost()) {
                $form->setInputFilter($calendar->getInputFilter());
                $form->setData($request->getPost());

                if ($form->isValid()) {
                    //$this->getCalendarTable()->saveCalendar($form->getData());
                    return $this->redirect()->toRoute('calendar', array('action' => 'show', 'id' => $id));
                }
            }

            return array(
                'id' => $id,
                'form' => $form,
            );
    }
Muhammad Arif
  • 1,014
  • 3
  • 22
  • 56

1 Answers1

0

You don't need to create a query to fetch by id as the DocumentRepository already provides this via it's find($id) method.

Just use the document manager to fetch the repository

$document = $documentManager->getRepository('FooDocument')->find($id);

You can also use the convenience method find($documentName, $id) directly on the document manager.

$document = $documentManager->find('FooDocument', $id);
AlexP
  • 9,906
  • 1
  • 24
  • 43
  • i updated my code see my code i used your code but error exist,i want to get data from calendar repository then bind this data to form and then edit the form how i do this? – Muhammad Arif Apr 18 '14 at 14:43
  • @MuhammadArif It's going to be really difficult for people to help you if you keep changing the original code; Nevertheless you are missing a line you already had in the original; `$documentManager = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');` As `$documentManager` is undefined. If this error was not already immediately obvious to you then I would highly recommend learning the ZF2 basics before you go any further. – AlexP Apr 18 '14 at 14:54