简体   繁体   English

Doctrine ODM查询生成器 - 查找集合为空的位置

[英]Doctrine ODM Query Builder - Find where collection is empty

I am trying to create a query, using the doctrine ODM query builder, where a referenced association ( eventListeners ) is not empty - or the collection has one or more item. 我正在尝试使用doctrine ODM查询构建器创建查询,其中引用的关联( eventListeners不为空 - 或者集合具有一个或多个项目。

The query: 查询:

$qb = $om->createQueryBuilder(FormService::ENTITY_CLASS_NAME_FORM);
$query = $qb->field('website.$id')->equals(new \MongoId($website->getId()))
            ->field('status.name')->equals(FormService::STATUS_PUBLISHED)
            ->field('eventListeners')->notEqual(array());
            ->getQuery();
$results = $query->execute();

I've been creative in my attempts with the API; 我在尝试使用API​​方面一直很有创意; This line is clearly incorrect as it still returns all the documents regardless 此行显然不正确,因为它仍然返回所有文档,无论如何

->field('eventListeners')->notEqual(array());

I can see in the documentation you can use field('eventListeners')->size(3); 我可以在文档中看到你可以使用field('eventListeners')->size(3); however I do not know in advance what the collection size should be. 但我事先并不知道收集的大小应该是多少。

How do you query for non empty collections using Doctrine ODM? 如何使用Doctrine ODM查询非空集合?

It's probably not the best way to achieve this, but it does work: 它可能不是实现这一目标的最佳方式,但它确实有效:

$qb = $om->createQueryBuilder(FormService::ENTITY_CLASS_NAME_FORM);
$query = $qb->field('website.$id')->equals(new \MongoId($website->getId()))
            ->field('status.name')->equals(FormService::STATUS_PUBLISHED)
            ->field('eventListeners.0')->exists(true)
            ->getQuery();

$results = $query->execute();

This assumes that it is a 0 based index Collection and not a Hash . 这假设它是基于0的索引Collection而不是Hash

I realise you can do DB.find({eventListeners: {$not: {$size: 0}}}) in mongo, but I'm not sure how to properly structure it in the ODM query builder. 我意识到你可以在DB.find({eventListeners: {$not: {$size: 0}}})中做DB.find({eventListeners: {$not: {$size: 0}}}) ,但我不确定如何在ODM查询构建器中正确构建它。

After reviewing the odm documentation I'm not sure if it's even possible to do. 在查看odm文档之后,我不确定它是否可行。

The other approach is to use a $where function: http://docs.mongodb.org/manual/reference/operator/query/where/ 另一种方法是使用$where函数: http//docs.mongodb.org/manual/reference/operator/query/where/

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

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