【问题标题】:writing join query query in zf2 and Doctrine2在 zf2 和 Doctrine2 中编写连接查询查询
【发布时间】:2014-03-27 07:08:52
【问题描述】:

我在 zf2 和学说 orm 中有一个查询。我通过它的 primary_key 加入了对同一个表的两个查询的结果。

看来,我犯了一个语法错误,无法找到。

$query = $this->getEntityManager()->createQuery(
    "select tc_result1.id as id ,tc_result1.displayId as displayId,tc_result1.activeFlag,tc_result1.hash
        from
        (
            SELECT *
            FROM (
                    SELECT id, display_id,active_flag,hash
                    FROM Test\Entity\TestCase tc_inner1
                    where tc_inner1.activeFlag=0 and tc_inner1.product = :productId
                    ORDER BY tc_inner1.displayId DESC
                 ) a
            GROUP BY hash 
        ) AS tc_result1

        join

        (
            SELECT *
            FROM (
                    SELECT id, displayId,activeFlag,hash
                    FROM Test\Entity\TestCase tc_inner2
                    where tc_inner2.activeFlag=0 and tc_inner2.product = :productId
                    ORDER BY tc_inner2.displayId DESC
                 ) a
            GROUP BY hash 
        ) AS tc_result2
        on
        tc_result1.id = tc_result2.id"
   );
$query->setParameter("productId", $productId);

我收到以下错误:

附加信息:Doctrine\ORM\Query\QueryException 文件: /var/www/test-suite/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:63

消息:

[Semantical Error] line 0, col 153 near '(': Error: Class '(' is not defined.

【问题讨论】:

    标签: php sql doctrine-orm zend-framework2


    【解决方案1】:

    我昨天在doctrine-user mailinglist 上对类似问题的回答也适用于这里:

    您在此错误做的是:定义 ON 子句。那 是你如何在 SQL 中做到这一点。但是,在 DQL 中,您可以定义关系 映射信息中的实体之间(通过注释, xml、yaml 或 php)并在查询中使用该关系。所以: 定义 OneToOne、OneToMany、ManyToOne 或 ManyToMany 关系和 在查询中使用它(因此不需要使用 ON)。

    和相关的(来自同一个线程):

    您使用 DQL 查询的不是表,而是实体!这 实体(对象)形成一个网络,即对象图。你能走” 那个图。查询中未定义关联(使用 ON 或 WHERE),但在映射中。这就是 ORM 的重点; 否则没有必要使用它,你最好只是 直接用SQL查询数据库。

    【讨论】:

      【解决方案2】:

      我遇到了问题。对实体类而不是表的教义查询。它需要一个实体类

      select tc_result1.id as id ,tc_result1.displayId as displayId,tc_result1.activeFlag,tc_result1.hash
                      from
                      (
      

      解决这个问题的方法是使用 ResultSetMappingBuilder 对象,我们可以使用它在原生 sql 中编写查询并将结果映射到学说对象

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        • 2018-11-19
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多