gpt4 book ai didi

doctrine-orm - "IN"带有标准过滤的谓词不起作用

转载 作者:行者123 更新时间:2023-12-02 03:18:05 25 4
gpt4 key购买 nike

我正在尝试使用 Criteria 直接过滤 Doctrine 集合当我只搜索某些元素时,为了避免所有集合加载。

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 = ?' with params [22,13]:



所以,这个错误

Notice: Array to string conversion



我想这里生成了错误的 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 手动运行该查询,我会得到我期望的)。

所以 ...

奖金问题

我可以用 Criteria如果我的集合尚未加载,则仅在实体 getter 或存储库中?

Edit3 - 奖金问题

似乎如果您还没有加载集合,则匹配条件是非常无用的。事实上,如果我循环到集合元素并调用一些 getter,然后,我使用条件,结果集合就是我正在搜索的(但是,当然,所有集合元素现在都已加载)

最佳答案

我找到了这个

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

在 ManyToMany 收集情况下,这似乎是一个“众所周知”的问题

关于doctrine-orm - "IN"带有标准过滤的谓词不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35358597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com