【问题标题】:Doctrine, QueryBuilder : use particular "where" with parenthesisDoctrine,QueryBuilder :使用带括号的特定“where”
【发布时间】:2014-04-17 10:11:06
【问题描述】:

我在使用 Doctrine QueryBuilder 进行“简单”查询时遇到问题。 我试着找一些最长 10 公里的“人”。

我的查询:

    $QB = $this->createQueryBuilder('p');
    $QB->add('select', 'p')
            ->add('from', 'MyProject\Bundle\FrontBundle\Entity\Pro p')
            ->where('p.job = :job')
            ->andWhere('(3956 * 2 * ASIN(SQRT( POWER(SIN((:latitude - abs(pro.latitude)) * pi()/180 / 2),2) + COS(:latitude * pi()/180 ) * COS(abs(pro.latitude) *  pi()/180) * POWER(SIN((:longitude - pro.longitude) *  pi()/180 / 2), 2) ))) <= 10')
            ->addOrderBy('p.dateCreation', 'DESC')
            ->addOrderBy('p.id', 'DESC')
            ->setParameter('latitude', $latitude)
            ->setParameter('longitude', $longitude)
            ->setParameter('job', $jobId);

问题出在第二个“where”语句上,由于后面的括号,Doctrine 在“ASIN”上失败了。它试图执行函数...... 有没有办法逃脱呢?或者另一种构造这个条件的方法?

感谢 Doctrine 专业人士 ;)

【问题讨论】:

    标签: postgresql symfony doctrine-orm query-builder


    【解决方案1】:

    如果我理解正确,您可以为此编写一个原始 SQL 查询,或者在学说中实现函数 ASIN(或任何其他)。

    这里有一个可能有帮助的捆绑包:

    https://github.com/wiredmedia/doctrine-extensions

    你可以使用它的ASIN功能实现:

    https://github.com/wiredmedia/doctrine-extensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Asin.php

    还有关于自定义 DQL 函数的文章:

    http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html

    【讨论】:

    • 正如我所说(或写的),您只需将提到的捆绑包添加到您的项目中,所有这些 DQL 功能都已经在那里实现,如 github.com/wiredmedia/… 中列出的那样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多