0

我正在尝试将 mysql 查询转换为 symfony2 中的学说。

这是直接的mysql查询。

SELECT * FROM referrals r
INNER JOIN domain d ON LOWER(d.domain) = LOWER(SUBSTRING_INDEX(r.email, '@', -1))
LEFT OUTER JOIN user_referrals ur ON ur.referral_id = r.referral_id AND ur.user_id = 1
WHERE LOWER(d.domain) = LOWER('Gmail.com') AND ur.referral_id IS NULL
ORDER BY r.suggestion_count ASC LIMIT 5;

这就是我对教义的转变。

$em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
            'Select r From FuelFormBundle:Referrals r
            INNER JOIN FuelFormBundle:Domain d WITH d.domain = LOWER(SUBSTRING_INDEX(r.email, "@", -1))
            LEFT OUTER JOIN FuelFormBundle:UserReferrals ur WITH ur.referral_id = r.referral_id AND ur.user_id = :userId
            WHERE LOWER(d.domain) = LOWER(:domain) AND ur.referral_id IS NULL
            ORDER BY r.suggestion_count ASC'
        )->setParameters(array(
                'domain', $domain['email'],
                'userId' => $currentUserId))
            ->setMaxResults(5);

        $referral = $query->getResult();

我在运行时收到的错误消息是。

[Syntax Error] line 0, col 110: Error: Expected known function, got 'SUBSTRING_INDEX'

堆栈跟踪是

1 error
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
INFO - Matched route "ajax_get_five_emails" (parameters: "_controller": "Fuel\FormBundle\Controller\DatabaseController::getFiveEmailsWithSameDomainAction", "_route": "ajax_get_five_emails")
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException".
CRITICAL - Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 110: Error: Expected known function, got 'SUBSTRING_INDEX'" at /vagrant/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 44
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ErrorsLoggerListener::injectLogger".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Acme\DemoBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
DEBUG - Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".

我打算尝试使用查询生成器,但是我似乎找不到如何进行左外连接以及与 Substring_index 相同的问题。

有任何想法吗?


编辑:

我已经替换substring_indexLOWER(SUBSTRING(r.email, LOCATE('@', r.email ,1))),到目前为止它看起来似乎接受了语法。


编辑2:

最后一个答案非常接近,但我有点偏离。答案是LOWER(SUBSTRING(r.email, LOCATE('@', r.email)+1))

如果我找出问题,LEFT OUTER JOIN我会发布它。
我确实找到了这个

Fetch Joins:除了使用常规连接之外:用于获取相关实体并将它们包含在查询的水合结果中。

没有特殊的 DQL 关键字可以区分常规连接和获取连接。只要连接实体的字段出现在聚合函数之外的 DQL 查询的 SELECT 部分中,连接(无论是内部连接还是外部连接)都会成为“获取连接”。否则,它是“常规加入”。

但我没有更多时间专注于它了。

这是上面引用的链接http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#dql-functions

4

0 回答 0