Doc中的Left Outer Join和Substring_Index

时间:2013-10-15 19:06:09

标签: php mysql symfony doctrine-orm

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

这是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_index替换为LOWER(SUBSTRING(r.email, LOCATE('@', r.email ,1))),目前它似乎接受了语法。


编辑2:

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

如果我通过LEFT OUTER JOIN找出问题,我会将其发布 我找到了这个

  

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

     

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

但我没有时间专心致志了。

以下是http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#dql-functions

上方引用的链接

0 个答案:

没有答案