【问题标题】:"IN" predicate with Criteria filtering isn't working带有条件过滤的“IN”谓词不起作用
【发布时间】:2016-02-12 09:07:36
【问题描述】:

我正在尝试使用Criteria 直接过滤一个学说集合,以避免在我只搜索某些元素时加载所有集合。

public function bar()
{
    $entity = ...; // not useful for the example
    $property = ...; // not useful for the example but is a getter for a collection
    $ids = [22, 13]; // just for the sake of this example
    $criteria = Criteria::create()->where(Criteria::expr()->in("id", $ids));

    return $this->foo($entity, $property, $criteria);
}

public function foo($entity, $property, Criteria $criteria)
{
    return $this->propertyAccessor->getValue($entity, $property)->matching($criteria);
}

以上代码将生成此 DQL(我将只显示最后和相关的部分)

AND te.id = ?'带参数 [22,13]:

所以,这个错误

注意:数组到字符串的转换

我想这里生成了错误的DQL,但我不知道为什么。

有人知道吗?


这是我要匹配的集合属性

/**
 * @ORM\ManyToMany(targetEntity="Vendor\Bundle\Entity\Foo")
 * @ORM\JoinTable(name="foo_bar",
 *     joinColumns={@ORM\JoinColumn(name="foo_bar_id", referencedColumnName="id", onDelete="cascade")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="foo_id", referencedColumnName="id")}
 * )
 */
 protected $foo;

编辑

如果我尝试转储 $criteria 对象,我会得到这个

Criteria {#10958 ▼
  -expression: Comparison {#10956 ▼
    -field: "id"
    -op: "IN"
    -value: Value {#10948 ▼
      -value: array:2 [▼
        0 => 22
        1 => 13
      ]
    }
  }
  -orderings: []
  -firstResult: null
  -maxResults: null
}

所以这似乎是正确的。我也知道我可以使用“或”表达式,但我更愿意了解为什么会出现此问题。

编辑2

我把$criteria改成如下

$criteria = Criteria::create()
    ->where(Criteria::expr()->eq('id', 22))
    ->orWhere(Criteria::expr()->eq('id', 13));

现在,查询是正确的,但集合不正确:它是空的,但不应该是(如果我从 mysql cli 手动运行该查询,我会得到我期望的结果)。

所以...

奖金问题

如果我的集合尚未加载,我能否仅在实体 getter 或存储库中使用 Criteria

Edit3 - 额外问题

看来,如果您还没有加载集合,那么匹配条件就没什么用了。事实上,如果我循环到集合元素并调用一些 getter,然后使用条件,则结果集合就是我要搜索的(当然,现在所有集合元素都已加载)

【问题讨论】:

    标签: doctrine-orm doctrine


    【解决方案1】:

    我找到了这个

    https://github.com/doctrine/doctrine2/issues/4910

    这似乎是多对多收集情况下的“众所周知”问题

    【讨论】:

      猜你喜欢
      • 2020-10-17
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 2016-07-03
      相关资源
      最近更新 更多