如何使用Doctrine Criteria过滤掉数组属性?

Iva*_*ica 6 php doctrine symfony doctrine-orm

我在Symfony实体类中添加了一个虚拟属性.此属性应基于另一个表数据计算 - 特别是在Doctrine数组类型的列上.

class RelatedEntity
{

    /* ... */

    /**
     * @ORM\Column(type="array")
     */
    protected $type;
Run Code Online (Sandbox Code Playgroud)

关键是我想使用Doctrine Criteria,因为它应该在SQL级别上进行优化.所以我这样做了:

public function getCreated()
{
    $criteria = Criteria::create()->where(Criteria::expr()->contains('type', 'create'));
    $relatedEntity = $this->getRelatedEntities()->matching($criteria);

    if (!$relatedEntity) {
        return null;
    }

    return $relatedEntity->getTimestamp();
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个空的结果集.即使Doctrine正在构建一个正确的SQL语句,当我手动将其键入PostgreSQL数据库时,它也能正常工作.

...WHERE type LIKE '%create%'

这种方法有什么问题,如何解决?现在我使用ArrayCollection过滤器方法完成了这个技巧,但它加载了我不需要的所有相关实体.

谢谢你的任何想法.

编辑:这不是上述问题的重复,因为我不能在实体内使用EntityManagerEntityRepository.我需要使用Criteria,因此问题中提出的解决方案对我不起作用.

Ton*_*cas 1

检查结果getRelatedEntities()

根据此集合的创建方式,可能会发生以下几种情况中的任何一种。特别是,它可能正在使用实体别名,或者可能不会返回任何与您的Criteria.

  1. 从别名实体填充的集合(即:通过 QueryBuilder 加入/选择)。
    • 如果getRelatedEntities由 Doctrine 通过 QueryBuilder 填充,您可能已经为实体添加了别名。
    • 前任。:$queryBuilder->addSelect('thing')->leftJoin('root_alias.entity', 'thing')
    • 在这种情况下,Criteria 必须使用别名:
      • Criteria::expr()->contains('thing.type', 'create')
  2. 没有符合条件的匹配项。
    • 在过滤之前转储您的集合,这可能是您的查询已经过滤掉任何潜在匹配项的简单情况。

测试你的标准

考虑到所有因素,在没有任何关于您要过滤的集合的结构的线索的情况下,我们只能评估您的标准。因此,测试您的标准,并检查您尝试过滤的集合的内容。

$criteria = Criteria::create()->where(Criteria::expr()->contains('type', 'create'));

$collection = new ArrayCollection([
    [
        'key' => 1,
        'type' => 'somethingcreatesomething',
    ],
    [
        'key' => 2,
        'type' => 'abra',
    ],
    [
        'key' => 3,
        'type' => 'cadabra',
    ],
    [
        'key' => 4,
        'type' => 'alacreate',
    ],
]);

dump($collection->matching($criteria));
Run Code Online (Sandbox Code Playgroud)

结果

Doctrine\Common\Collections\ArrayCollection {#2536
  -elements: array:2 [
    0 => array:2 [
      "key" => 1
      "type" => "somethingcreatesomething"
    ]
    3 => array:2 [
      "key" => 4
      "type" => "alacreate"
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)