Doctrine ODM - 通过embedMany集合实体获取实体

时间:2014-06-20 14:21:20

标签: php mongodb symfony doctrine-orm odm

我有embedMany字段“properties”

的集合
{
    "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)
            );
        }
    }
}

但它不起作用

1 个答案:

答案 0 :(得分:0)

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