2014-02-25 29 views
0

我在zf2和doctrine orm中查詢。我通過其primary_key參與同一個表上的兩個查詢的結果。在zf2和Doctrine2中寫入連接查詢查詢

看來,我犯了一個語法錯誤,無法找出答案。

$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); 

我得到了以下錯誤:

Additional information: Doctrine\ORM\Query\QueryException File: /var/www/test-suite/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php:63

Message:

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

回答

3

我昨天給了一個類似的問題在doctrine-user mailinglist答案也適用於此:

What you are doing wrong here is: defining an ON-clause. That is how you'd do it in SQL. In DQL however you define the relationship between your entities in the mapping-information (via annotations, xml, yaml or php) and in the query you use that relationship. So: define a OneToOne, OneToMany, ManyToOne or ManyToMany relationship and use that in the query (and consequently no need to use a ON).

及相關(在同一個線程):

you are not querying the tables with DQL, but the entities! The entities (objects) form a network, the object graph. You can "walk" that graph. The associations are not defined in the query (with ON or WHERE), but in the mapping. That is the whole point of an ORM; otherwise there is no need to use it and you could better just directly query the database with SQL.

1

我得到了問題。學說查詢到實體類而不是表格。該公司預計後

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

解決這個問題的一個實體類使用ResultSetMappingBuilder對象,使用它我們可以寫在本地SQL查詢和映射結果教義對象