2012-01-19 2 views
0

私は3つのテーブルを持っています。 2つは、データを取得する外部キーとデータを表示する外部キーを使用します。symfonyのループで外部キーを使用する

私のテーブルは、このようなものです:

department: 
id: 1 name: illustration 
id: 2 name photography 
etc... 

type: 
id: 1 name: ink 
id: 2 name: charcoal 
etc... 

user: 
id: 1 firstname: Fred lastname: Flintstone 
id: 2 firstname: Barney lastname: Rubble 
etc.... 

だから私は、行の種類やユーザーデータを部署に従った順序でデータを表示したいです。例えば

:私はこれまでの作品を持って何

Illustration 
ink  Fred Flintstone 
charcoal Fred Flintstone 
painting Fred Flintstone 

Photography 
ink  Barney Rubble 
painting Barney Rubble 

、私はそれがはるかに優れて実行することができると思います。

public function executeIndex(sfWebRequest $request) 
    { 
    $this->illustration = Doctrine_Core::getTable('User') 
     ->createQuery('a') 
     ->where('a.department_id = 1') 
     ->execute(); 

    $this->photography = Doctrine_Core::getTable('User') 
     ->createQuery('a') 
     ->where('a.department_id = 2') 
     ->execute(); 
} 

とインデックス アプリケーション/フロントエンド/モジュール/ fooの/テンプレート/ indexSuccessで

アプリケーション/フロントエンド/モジュール/ fooの/アクション/ actions.class.phpをする:これはwhatIが機能することを持っています。 php

<?php foreach ($illustration as $user): ?> 
     <tr> 
     <td class="displayDept" valign="top"><?php echo $user->getDepartment() ?></td> 
    </tr> 
    <tr> 
    <?php foreach ($illustration as $user): ?> 
    <tr> 
     <td class="displayInfo" valign="top"><?php echo $user->getType() ?></td> 
     <td class="displayInfo" valign="top"><?php echo $user->getUrl() ?></td> 
     <td class="displayInfo" valign="top"><?php echo simple_format_text($user->getDescription()) ?></td> 
     <td class="displayInfo" valign="top"><?php echo $user->getDateTimeObject('created_at')->format('m/d/Y') ?></td> 
    </tr> 
    <?php endforeach; ?> 

    <?php endforeach; ?> 
    <tr> 
    <?php foreach ($photography as $me): ?> 
    <tr> 
     <td class="displayDept" valign="top"><?php echo $me->getDepartment() ?></td> 
    </tr> 
    <?php foreach ($photography as $me): ?> 
    <tr> 
     <td class="displayInfo" valign="top"><?php echo $me->getType() ?></td> 
     <td class="displayInfo" valign="top"><?php echo $me->getUrl() ?></td> 
     <td class="displayInfo" valign="top"><?php echo simple_format_text($me->getDescription()) ?></td> 
     <td class="displayInfo" valign="top"><?php echo $me->getDateTimeObject('created_at')->format('m/d/Y') ?></td> 
    </tr> 
    <?php endforeach; ?> 
    <?php endforeach; ?> 

ここで、これは機能しますが、部門リストが本当に長くなる場合はどうなりますか。つまり、アクションで新しいデータベースクエリを作成し、indexSuccess.phpファイルでforeachオプションを使用する必要があります。

行内の後続データのWHEREオプションを使用して部門データをすべて取得する方法はありますか?ここでUPDATE

は、私は@ManseUKが提案するものにかなり近いと思いスキーマ、次のとおりです。と言って、あなたのschema.ymlを見ずにその困難

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

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 } 
    Type: { class: Type, local: type_id, foreign: id, foreignAlias: Users } 

答えて

1

- しかし、あなたは持っている場合セットアップスキーマ内の別名が正しく、あなたのような何かを行うことができる必要があります:

actions.phpを

public function executeIndex(sfWebRequest $request) 
{ 
    $this->departments = Doctrine_Core::getTable('departments') 
     ->findAll(); 
    // returns all departments or you could change this to just select departments by name/id 
} 

indexSuccess.phpと

<?php foreach ($departments as $dept): ?> 
     <tr> 
     <td 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"><?php echo $user->getUrl() ?></td> 
     <td class="displayInfo" valign="top"><?php echo simple_format_text($user->getDescription()) ?></td> 
     <td class="displayInfo" valign="top"><?php echo $user->getDateTimeObject('created_at')->format('m/d/Y') ?></td> 
    </tr> 
    <?php endforeach; ?> 
    <?php endforeach; ?> 

schema.ymlの

Department: 
    columns: 
    id: 
     type: integer 
     primary: true 
     autoincrement: true 
    name: String(25) 
Type: 
    columns: 
    id: 
     type: integer 
     primary: true 
     autoincrement: true 
    name: String(25) 
User: 
    columns: 
    id: 
     type: integer 
     primary: true 
     autoincrement: true 
    firstname: string(25) 
    lastname: string(25) 
    department: integer 
    type: integer 
    relations: 
     Department: 
     class: Department 
     local: department 
     foreign: id 
     foreignAlias: Users 
     Type: 
     class: Type 
     local: type 
     foreign: id 
     foreignAlias: Users 

foreignAlias paramがここで重要である - それは、テーブル間のリレーションシップ名を具体的 - あなたが$dept->getUsers()を呼び出すことができるように - すべてのユーザーを返します特定の部門の

+0

私はあなたの提案を使用し、このエラーを投げた: ** Unknow "Department"のプロパティ/関連コンポーネント "users" ** –

+0

@CareyEstesクラスを再構築しましたか(doctrine:build --all-classes)? – ManseUK

+0

はい、キャッシュをクリアしました。私はforeignAliasを持っていないことに気付きました:部門関係のための私のスキーマのユーザ(上記のスキーマ参照)。これはエラーを投げていますか?私は以来foreignAliasを追加しました:ユーザーはまだ運がありません。同じ未知のレコードプロパティーエラーを取得する –

関連する問題