douyu1656 2014-04-29 08:46
浏览 40

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

I'm unable to sort the embedded document. Here is my code.

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

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

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?

  • 写回答

1条回答 默认 最新

  • douju2012 2016-12-26 08:06
    关注

    you can't sort embedded document in mongodb (respectively in doctrine 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). 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. big minus - the result may be difference from original document and can't hydrate to ODM class(problem with mapping to document). use this case if documents is too many and/or result stored temporary (e.g. only showing in template)

    PS. no sorting command in your code ;)

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line