2016-08-08 2 views
2

DQL의 정규 LEFT JOIN을 통해 3 개의 엔티티를 선택합니다. 그것들은 엔티티가 정의 된 엔티티와 주석이 달린 관계가있는 조인 테이블을 통해 관련됩니다. 쿼리는 문제없이 실행되지만 결과는 플랫 배열로 반환됩니다. 내가 객체 나 배열로 중 수분, 쿼리에 getResult()를 호출 할 때Doctrine DQL 조인에서 플랫 배열을 반환합니다.

SELECT e1, e2, e3 FROM AppBundle:EntityOne 
JOIN AppBundle:JoinEntityOneWithTwo e1_2 WITH e1_2.entity1 = e1.id 
JOIN AppBundle:EntityTwo e2 WITH e1_2.entity2 = e2.id 
JOIN AppBundle:JoinEntityOneWithThree e1_3 WITH e1_3.entity1 = e1.id 
JOIN AppBundle:EntityThree e3 WITH e3.id = e1_3.entity3 
WHERE e1.some_field IN ('some','values','in','e1'); 

, 나는 평평한 결과를 설정 얻을 : 나는 각 인덱스에 대한 배열 요소로 세 가지 기관과 배열을 기대

array(
/AppBundle/Entity/EntityOne (...), 
/AppBundle/Entity/EntityTwo (...), 
/AppBundle/Entity/EntityThree (...), 
/AppBundle/Entity/EntityOne (...), 
/AppBundle/Entity/EntityTwo (...), 
/AppBundle/Entity/EntityThree (...), 
/AppBundle/Entity/EntityTwo (...), 
/AppBundle/Entity/EntityThree (...), 
/AppBundle/Entity/EntityOne (...), 
) 

나는 기대, 또는 다중 차원 배열하고 싶은 : 결과는 행으로 관련이없는

Array(
[0] => Array(
    [0] /AppBundle/Entity/EntityOne, 
    [1] /AppBundle/Entity/EntityTwo, 
    [2] /AppBundle/Entity/EntityThree 
), 
[1] => . . . 
) 

합니다. 그들은 예측 가능한 순서대로 그룹화 할 수 없습니다. array_chunk()

생성 된 SQL은 정상적으로 실행됩니다. DQL은 정확한 결과를 반환합니다. 단지 기대했던 방식으로 공식화되지 않았습니다.

http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

이 질문은

Doctrine DQL returns multiple types of entities

과 매우 유사하지만 내 선택하기 위해 내 테이블을 조인 많은 -만큼 다릅니다 열망 로딩에 나는 교리를 다음입니다 (도비) 문서 조인 다 대다. 감사합니다.

이 질문의 변형는 질문을 받았다 :

Getting Doctrine DQL results the SQL way

나는 교리 테이블 조인을 통해 열망로드를 지원하지 않는 꽤 놀랐어요.

+0

당신이 선택해야 열망로드를 사용하려면 E1 만하지 E1, E2 및 e3. – Alsatian

+0

예, e1에는 테이블에 외래 키 참조가 없습니다. 나는 e1_2를 선택함으로써 그 길의 일부를 얻을 수 있었지만 나는 e3와의 관계를 잃어 버렸다. – eggmatters

답변

1

는 여기에 내가 가지고 올 수있는 최고 :

//in my entity repository: 
public function getFoo($hydrate = true) { 
    $sql = "SELECT e1, e2, e3 FROM entity_one 
      JOIN entity_one_entity_two e1_2 ON e1_2.entity_one_id = e1.id 
      JOIN entity_two e2 ON e1_2.entity_two_id = e2.id 
      JOIN entity_one_entity_three e1_3 ON e1_3.entity_one_id = e1.id 
      JOIN entity_three e3 ON e3.id = e1_3.entity_three.id 
      WHERE e1.some_field IN ('some','values','in','e1')"; 
    $stmt = $con->prepare($sql); 
     $stmt->execute(); 
    return ($hydrate) 
    ? $this->hydrateResponses($stmt->fetchAll(PDO::FETCH_ASSOC)) 
     : $stmt->fetchAll(PDO::FETCH_ASSOC); 
} 

public function hyrdateResponses($responses) { 
    //call find by ids on repositories from array. 
} 

하나만에서 결과를 얻을려고 할 때 3XN에게 쿼리 수를 수행하고 있기 때문에 이것은 좋은 작품!

0

전화 getScalarResult() 대신 쿼리에 getResult() 및 모든의 모든 필드가 테이블에 합류 얻을 것이다 레코드 당 하나 개의 배열로 병합 :

# Replace this call ... 
$query->getQuery()->getResult(); 
# ... by that call ... 
$query->getQuery()->getScalarResult();