Regex与Doctrine 2查询构建器? [英] Regex with Doctrine 2 query builder?

查看:132
本文介绍了Regex与Doctrine 2查询构建器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标题,如何使用Doctrine 2查询构建器匹配正则表达式?基本上我试图产生独特的s。声。



这是我目前的实现。我产生s子然后我检查看是否有这样的s in使用的s。。如果有的话,我会附加一个 - {number}到s the的最后,其中{number}是最近没有使用的数字。

 code> $ qb-> select(array('partial o。{id,slug}'))
- > from('Foo\Bar\Entity\Object','o ')
- > where($ qb-> expr() - > like('o.slug',':slug'));

$ slug = new SlugNormalizer($ text);
$ qb-> setParameter('slug',$ slug-> __ toString().'-%');

这里的问题是LIKE slug%可以匹配foo-bar-1,foo-bar-2,和foo-bar-not-the-same-slug。更清晰的是正则表达式寻找REGEX slug-(\d +)或类似的东西。



使用Doctrine 2查询构建器执行此操作的任何方法? p>

解决方案

添加DoctrineExtensionsBundle - 更新您的composer.json:

 beberlei / DoctrineExtensions:0.3.x-dev,

添加REGEXP配置 - 更新您的app / config.yml

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

你的QueryBuilder在哪里这样做:

  $ qb = $ this-> createQueryBuilder('x'); 

return $ qb-> andWhere('REGEXP(x.your_property,:regexp)= true')
- > setParameter('regexp','[[:digit:] ] {3}')//在这里插入你自己的正则表达式
- > getQuery() - > getResult();

,不要忘记使用SQL兼容的正则表达式


As per the title, how would one match on a regular expression with the Doctrine 2 query builder? Basically I'm trying to generate unique slugs.

Here is my current implementation. I generate the slug. I then check to see if there are any slugs in use like this slug. If there are, I will append a -{number} to the end of the slug where {number} is the lowest number not already in use.

$qb->select(array('partial o.{id, slug}'))
   ->from('Foo\Bar\Entity\Object', 'o')
   ->where($qb->expr()->like('o.slug', ':slug'));

$slug = new SlugNormalizer($text);
$qb->setParameter('slug', $slug->__toString().'-%');

The problem here is LIKE slug% could match foo-bar-1, foo-bar-2, AND foo-bar-not-the-same-slug. What would be cleaner is a regex looking for REGEX slug-(\d+) or something similar.

Any way to do this with the Doctrine 2 query builder?

解决方案

add the DoctrineExtensionsBundle - update your composer.json:

"beberlei/DoctrineExtensions": "0.3.x-dev",

add the REGEXP configuration - update your app/config.yml

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

where ever your QueryBuilder is do this:

$qb = $this->createQueryBuilder('x');

return $qb->andWhere('REGEXP(x.your_property, :regexp) = true')
          ->setParameter('regexp', '[[:digit:]]{3}') // insert your own regex here
          ->getQuery()->getResult();

and don't forget to use SQL compatible regexes

这篇关于Regex与Doctrine 2查询构建器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆