2012-01-20 1 views
1

부서, 유형 및 사용자의 세 테이블에서 가져온 목록이 있습니다. Symfony 1.4에서 설정 한 foreignAlias로만 행을 표시하는 방법

는 다음과 같이 표시된다 :

Film 
student  Jimmy Stewart 
faculty  Cary Grant 

에서 indexSuccess.php

$this->departments = Doctrine_Core::getTable('Department') 
     ->createQuery('a') 
     ->orderBy('a.name') 
     ->execute(); 

actions.class.php

<?php foreach ($departments as $dept): ?> 
     <tr> 
     <?php check_for_id() ?> 
     <td colspan="4" class="displayDept" valign="top"><?php echo $dept->getName() ?></td> 
    </tr> 
    <tr> 
    <?php foreach ($dept->getUsers() as $user): ?> 
    <tr> 
     <td class="displayInfo" valign="top"><?php echo $user->getType() ?></td> 
     <td class="displayInfo" valign="top"><a href="<?php echo $user->getUrl() ?>" target="_blank"><?php echo $user->getUrl() ?></a></td> 
     <td class="displayInfo" valign="top"><?php echo simple_format_text($user->getDescription()) ?></td> 
    </tr> 
    <?php endforeach; ?> 
    <?php endforeach; ?> 

그러나이 당김의 foreach 초래 모든 이드 부서 테이블 아래에 빈 행이 남습니다. 이와 같이 :

Photography 
student  Keanu Reeves 
faculty  Lawrence Fishburn 

Drawing 

Film 
    student  Jimmy Stewart 
    faculty  Cary Grant 

Painting 

Sculpture 

사용자 ID가없는 부서를 제거 할 수 있습니까?

내가 조인 사용하기 위하여려고하는 경우

이 어떻게 행동에 있음을 부르죠 UPDATE? 지금까지 나는 곳이다,하지만 내 반복 전혀 작동하지 않습니다 :

$this->departments = Doctrine_Core::getTable('Department') 
     ->createQuery('a') 
     ->leftJoin('a.Users p') 
     ->orderBy('a.name ASC') 
     ->execute(); 

나는 그것이 비록 무엇을하는지 모르겠어요. 나는 그것이 전통적인 php에서와 같이 어디에서 왔는지 이해할 수 있지만 symfony 구문을 따를 수는 없습니다. 내가 닫을지라도 ??

답변

1

왜 쿼리를 수행하지 않아도 부서가 사용자와 합류 했습니까?

UPDATE

이 내 머리의 상단에서이지만, 이런 식으로 뭔가해야한다 :

$this->departments = Doctrine_Core::getTable('Department') 
     ->createQuery('a') 
     ->leftJoin('a.Users p') 
     ->groupBy('a.id') 
     ->having('COUNT(a.id) > 0') 
     ->orderBy('a.name ASC') 
     ->execute(); 
+0

오류는 발생하지 않지만 사용자 데이터가 비어있는 행은 제거되지 않습니다. 나는 "a"와 "p"가 나타내는 것을 이해하지 못한다고 생각합니다. schema.yml을 편집해야합니까? –

+0

a는 부서 테이블의 별명이고 users 테이블의 p입니다. –

0

블라드가 바로 내가 몇 개조하면 되겠 어를 만들어 게시하고 싶어했다 다른 사람들을 위해서.

여기에 나는 또한 내 schema.yml 파일

Department: 
actAs: { Timestampable: ~ } 
columns: 
    id: { type: integer(4), primary: true, autoincrement: true } 
    name: { type: string(255), notnull: true, unique: true } 
relations: 
    User: {class: User, local: name, foreign: department_id, foreignAlias: Departments} 

Type: 
actAs: { Timestampable: ~ } 
columns: 
    id: { type: integer(4), primary: true, autoincrement: true } 
    name: { type: string(255), notnull: true, unique: true } 
    user_id: { type: integer(4) } 

User: 
actAs: { Timestampable: ~ } 
columns: 
    id: { type: integer(4), primary: true, autoincrement: true } 
    firstname: { type: string(255), notnull: true } 
    lastname: { type: string(255), notnull: true } 
    email: { type: string(255), notnull: true, unique: true } 
    department_id: { type: integer(4), notnull: true } 
    type_id: { type: integer(4), notnull: true } 
    url: { type: string(255), notnull:true } 
    description: { type: string(4000) } 
relations: 
    Department: { class: Department, local: department_id, foreign: id, foreignAlias: Users} 
    Type: { class: Type, local: type_id, foreign: id, foreignAlias: Users } 

편집 그리고이 위대한 일을

$this->departments = Doctrine_Query::create() 
     ->select('d.id') 
     ->from('Department d') 
     ->leftJoin('d.Users u') 
     ->groupBy('d.id') 
     ->having('COUNT(u.id) > 0') 
     ->orderBy('d.name ASC') 
     ->execute(); 

actions.class.php에게 있습니다. 별칭 문자에 대한 자세한 설명을 주셔서 감사합니다. 이 글자들이 막 만들어 졌기 때문에 아직도 안개가 낀다. 나는 부서와 사용자를 위해 d를 선택했고, 이것이 차이가 있는지 확실하지 않습니다. 도와 주셔서 감사합니다 @ Vlad!

+0

편지는 아무런 차이가 없습니다. 부서를위한 D는 괜찮습니다. –

관련 문제