简体   繁体   English

原则在simple_array字段中找到数字

[英]Doctrine find Number in simple_array field

I have an entity Customer with an simple_array field processNumbers . 我有一个具有simple_array字段processNumbers Customer实体。 So in the DB its saved as an comma-separated list for example: 45645,78787,1111 . 因此,在数据库中将其另存为以逗号分隔的列表,例如: 45645,78787,1111 Now I want to find an entry with a specific number processNumber = 1111 . 现在,我想找到一个具有特定编号processNumber = 1111的条目。 I want to do it with the query builder, but I only know the direction when I have an array and I search if a DB entry is in it. 我想使用查询生成器来做到这一点,但是我只有在拥有数组并搜索其中是否包含数据库条目时才知道方向。 This direction doesn't work, it says that there is no entry, but it is: 这个方向不起作用,它说没有入口,但是它是:

$customer = $queryBuilder
            ->select('customer')
            ->from(Customer::class,'customer')
            ->where(":processNumber IN (customer.processNumbers)")
            ->setParameter('processNumber', $processNumber)
            ->getQuery()->getResult();

If you are using Mysql, you can use the Regex Expression in the DoctrineExtension 如果使用的是Mysql,则可以在DoctrineExtension中使用正则表达式

doctrine:
    orm:
        dql:
            string_functions:
                regexp: DoctrineExtensions\Query\Mysql\Regexp

Then use the regex 然后使用正则表达式

$qb->andWhere('REGEXP(customer.processNumbers, :regexp) = true')
          ->setParameter('regexp', '(^1111,)|(,1111,)|(,1111$)|(^1111$)')

My regex is very basic but understable (well I hope). 我的正则表达式非常基础,但性能很不稳定(我希望如此)。 You can see the regex matching here: https://regex101.com/r/oNqpUf/1 您可以在此处查看匹配的正则表达式: https : //regex101.com/r/oNqpUf/1

You can also do the following: 您还可以执行以下操作:

$qb->andWhere("customer.processNumbers LIKE :key1 OR customer.processNumbers LIKE :key2 OR customer.processNumbers LIKE :key3 OR customer.processNumbers LIKE :key4");
$qb->setParameter("key", "1111");
$qb->setParameter("key2", "%,1111");
$qb->setParameter("key3", "1111,%");
$qb->setParameter("key4", "%,1111,%");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM