【问题标题】:how to get single result using mongo odm?如何使用 mongo odm 获得单个结果?
【发布时间】:2014-06-03 01:06:46
【问题描述】:

我做了一个设置操作,其中我使用教义查询来获取单个结果,但是当数据库中存在数据时它返回 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,
            );
    }

【问题讨论】:

    标签: php mongodb zend-framework2 odm


    【解决方案1】:

    您不需要创建查询来按 id 获取,因为 DocumentRepository 已经通过它的 find($id) 方法提供了这个。

    只需使用文档管理器来获取存储库

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

    您也可以直接在文档管理器上使用便捷方法find($documentName, $id)

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

    【讨论】:

    • 我更新了我的代码查看我的代码我使用了你的代码但存在错误,我想从日历存储库中获取数据,然后将此数据绑定到表单,然后编辑表单我该怎么做?
    • @MuhammadArif 如果您不断更改原始代码,人们将很难帮助您;尽管如此,您还是缺少原件中已有的一行; $documentManager = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default'); 因为 $documentManager 未定义。如果这个错误对您来说还不是很明显,那么我强烈建议您在继续之前学习 ZF2 基础知识。
    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多