2012-01-16 4 views
2

아마도 Dotrine 자체보다 SQL에 더 관련이 있습니다. 는 이름으로 정렬 작업을,이다의 나는 목록이 프로젝트를 좀하고 싶습니다쿼리 결과에서 관련 (조인) 테이블의 필드를 정렬하는 방법은 무엇입니까?

Project: 
    columns: 
    name: { type: string(255), notnull: true } 

Task: 
    columns: 
    project_id: { type: integer, notnull: true } 
    name: { type: string(255), notnull: true } 
    relations: 
    Project: { onDelete: CASCADE, local: project_id, foreign: id, foreignAlias: Tasks } 

:

나는 대략 다음과 같이 스키마 파일에 지정된 두 개의 테이블을 가지고있다.

$projectWithTasks = Doctrine_Core::getTable("Project")->createQuery("p") 
    ->leftJoin("p.Tasks t") 
    ->where("p.id = ?", $projectId) 
    ->orderBy("t.name ASC") 
    ->fetchOne(); 

분명히 작동하지 않습니다. 나는 꽤 많은 시간 동안 해결책을 찾고 있었지만, 아마도 유용한 정보를 찾을 수 없었기 때문에 아마 잘못된 단어를 사용하고있을 것입니다. 어떤 도움을 주셔서 고맙게 생각합니다.

+0

흠, 이상합니다. 왜 이것이 작동하지 않을지 모르겠다. 대안으로, 항상 동일한 조건으로 정렬하려는 경우 매핑에서 순서를 지정할 수 있습니다. 이 예제를 보자. http://www.doctrine-project.org/blog/cookbook-recipe-relation-dql-behavior – ZolaKt

+0

글쎄, 내가 제대로 작동하지 않을 것이라고 잘못 생각한 것처럼 보인다.이 간단한 것으로 충분할 것 같다. 예. – Przemek

답변

1

당신은 당신의 YML 파일에 프로젝트의 관계, 예컨대 :

Project: 
    columns: 
    name: { type: string(255), notnull: true } 
    relations: 
    Task: 
     local: id 
     foreign: project_id 
     orderBy: name 

Task: 
    columns: 
    project_id: { type: integer, notnull: true } 
    name: { type: string(255), notnull: true } 

프로젝트를 통해 입수 할 때 작업이 자동으로 이름으로 분류되어이 방법으로 작업의 기본 순서를 정의 할 수 있습니다.

1

당신이 당신의 쿼리에서 선택 "t"를 사용하려는 경우, 당신은 선택 방법을 정의한다 :

$projectWithTasks = Doctrine_Query::create() 
    ->select('p.*, t.*') 
    ->from('Project p') 
    ->leftJoin('p.Tasks t') 
    ->where('p.id = ?', $projectId) 
    ->orderBy('t.name ASC') 
    ->fetchOne(); 

가 또는 당신은 단지에 의해 해 orderBy에서 "t"를 대체, 당신의 구문을 사용할 수 있습니다 "p.Tasks":

$projectWithTasks = Doctrine_Core::getTable("Project")->createQuery("p") 
    ->leftJoin("p.Tasks t") 
    ->where("p.id = ?", $projectId) 
    ->orderBy("p.Tasks.name ASC") 
    ->fetchOne(); 
관련 문제