简体   繁体   English

如何使用mongo odm获得单个结果?

[英]how to get single result using mongo odm?

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: 我进行了设置操作,其中我使用了主义查询来获取单个结果,但是当数据库中存在数据时它返回null,如何获取单个结果?这是我的代码:

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

You don't need to create a query to fetch by id as the DocumentRepository already provides this via it's find($id) method. 您无需创建查询即可按id进行获取,因为DocumentRepository已经通过它的find($id)方法提供了此查询。

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. 您还可以直接在文档管理器上使用便捷方法find($documentName, $id)

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

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

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