0

我有 embedMany 字段“属性”的集合

{
    "title": "Castle in Scotland",
    "properties": {
      "0": {
        "_id": NumberInt(13),
        "propType": {
          "$ref": "listing_property_types",
          "$id": NumberInt(9),
          "$db": "real_estate" 
        },
        "propOption": {
          "$ref": "listing_property_options",
          "$id": NumberInt(13),
          "$db": "real_estate" 
        } 
      },
      "1": {
        "_id": NumberInt(15),
        "propType": {
          "$ref": "listing_property_types",
          "$id": NumberInt(10),
          "$db": "real_estate" 
        },
        "propOption": {
          "$ref": "listing_property_options",
          "$id": NumberInt(15),
          "$db": "real_estate" 
      }
    } 
}

如果我想获取具有 propType.$id=9 和 propOption=13 属性的实体,如何构建查询

我试试这个

$builder = $this->createQueryBuilder()->select();
foreach ($propertiesArr as $propTypeId => $propOptId) {
    if (intval($propTypeId) > 0 && intval($propOptId) > 0) {
        $builder->addOr(
            $builder->expr()
                ->field('properties.propType.$id')->equals($propTypeId)
                ->field('properties.propOption.$id')->equals($propOptId)
            );
        }
    }
}

但它不起作用

4

1 回答 1

0

此代码段不起作用,因为 $propOptId 变量不是整数。如果将 $propTypeId 和 $propOptId 包装在 intval 中,则此代码将正常工作。

于 2014-06-25T19:59:32.363 回答