【问题标题】:Convert SQL to Doctrine将 SQL 转换为 Doctrine
【发布时间】:2015-05-27 17:41:19
【问题描述】:

我想将此sql查询转换为实体管理器的学说:

SELECT count(c.id)
        FROM content c
        WHERE c.id IN
            (SELECT tdtc.content_id
             FROM tdtheme_content tdtc
             JOIN tdtheme tdt ON tdtc.tdtheme_id = tdt.id
             JOIN td t ON tdt.td_id = t.id
             JOIN matiere m ON t.matiere_id = m.id
             WHERE m.id = 2)

        OR c.id IN
            (SELECT sc.content_id
             FROM semaine_content sc
             JOIN semaine s ON sc.semaine_id = s.id
             JOIN cour cr ON s.cour_id = cr.id
             JOIN matiere m ON cr.matiere_id = m.id
             WHERE m.id = 2)

        OR c.id IN
            (SELECT ac.content_id
             FROM annale_content ac
             JOIN annale a ON ac.annale_id = a.id
             JOIN matiere m ON a.matiere_id = m.id
             WHERE m.id = 2)');

我有:

Annale 和 Td 和 Cours 的 Matiere

有内容的年鉴

Td With Tdtheme With Contents

Courses With Semaines with Contents

我想计算一个 matiere 的内容数

有什么想法吗?

感谢和抱歉我的英语不好^^

【问题讨论】:

  • 欢迎来到 Stack Overflow!这个问题的信息有点少。能分享一下你尝试过的,遇到了什么问题吗?

标签: php symfony doctrine-orm doctrine


【解决方案1】:

假设您的实体名称是 ContentTdTheme_Content 等。

$repo = $em->getRepository('AppBundle:Content');
$query = $repo ->createQueryBuilder('c');

$subQuery1 = $query->createSubquery();
$subQuery1
    ->select('tdtc.contentId')
    ->from('TdTheme_Content tdtc')
    ->join('tdtc.tdTheme tdt') // Doctrine joins by property names, not column names
    ->join('tdt.td t')
    ->join('t.matiere m')
    ->where('m.id = ?', 2)
;

// Assume similar for $subQuery2 and $subQuery3

$query
    ->select('COUNT(c.id)')
    ->where('c.id IN(' . $subQuery1->getDql() ')')
    ->orWhere('c.id IN(' . $subQuery2->getDql() ')')
    ->orWhere('c.id IN(' . $subQuery3->getDql() ')')
;

注意:我没有对此进行调试或尝试执行它 - 这只是我对学说查询构建器 api 的一些基本知识的想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多