简体   繁体   English

使用Doctrine ODM在Symfony2中对嵌入式文档进行排序

[英]Sorting embedded document in Symfony2 with Doctrine ODM

I'm unable to sort the embedded document. 我无法对嵌入式文档进行排序。 Here is my code. 这是我的代码。

Children.php Children.php

namespace Acme\CCBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

/**
 * @MongoDB\Document
 */
class Children {
/..
    /**
     * @MongoDB\EmbedMany(targetDocument="Vaccine")
     */
    protected $vaccine = array();
}

Vaccine.php Vaccine.php

namespace Acme\CCBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 * @MongoDB\EmbeddedDocument
 */
class Vaccine
{
    /**
     * @MongoDB\Date
     */
    protected $recordDate;

    /**
     * @MongoDB\String
     */
    protected $vaccineName;
}

In my controller: RecordController.php 在我的控制器中:RecordController.php

public function showVaccineAction($id) {
        $child = $this->get('doctrine_mongodb')
                ->getRepository('AcmeCCBundle:Children')
                ->find($id);

        $vac = $child->getVaccine();

        return $this->render(
                        'AcmeCCBundle:Record:show_vaccine.html.twig', array('vac' => $vac)
        );
    }

I just be able query all the vaccine of a child, and it's not ordered. 我只是可以查询孩子的所有疫苗,而没有订购。 Can anybody help me? 有谁能够帮助我?

you can't sort embedded document in mongodb (respectively in doctrine odm). 您不能在mongodb中对嵌入式文档进行排序(分别在oddrine odm中)。

you can use two option: 您可以使用两个选项:

  1. update embedded document with sorting - you'll recreate documents, so no need to sorting(by default mongodb return documents sorted by created order). 通过排序更新嵌入式文档-您将重新创建文档,因此无需排序(默认情况下,mongodb返回按创建顺序排序的文档)。 use this case if documents is not many and/or storing sorted result permanently 如果文档不多和/或永久存储排序结果,请使用这种情况

  2. use aggregation in mongodb -you'll denormalize by embedded document and sort by any way. 在mongodb中使用聚合-您将通过嵌入式文档进行非规范化并以任何方式进行排序。 big minus - the result may be difference from original document and can't hydrate to ODM class(problem with mapping to document). 大负号-结果可能与原始文档有所不同,并且无法合并为ODM类(映射到文档的问题)。 use this case if documents is too many and/or result stored temporary (eg only showing in template) 如果文档太多和/或结果临时存储(例如仅显示在模板中),请使用这种情况

PS. PS。 no sorting command in your code ;) 您的代码中没有排序命令;)

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

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