2012-01-19 72 views
0

我有三个表。两个使用一个外键来提取数据,一个显示数据。在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 

我至今的作品,但我认为它可以执行好得多。这是whatI有工作:

应用/前端/模块/富/动作/的actions.class.php

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(); 
} 

和索引 应用/前端/模块/富/模板/ indexSuccess。 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建议:

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

没有看到你的schema.yml其很难说 - 但如果你有在模式中正确设置别名,那么你应该可以做类似的事情:

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参数是这里的关键 - 它的细节表之间的关系的名字 - 让你打电话$dept->getUsers() - 返回所有用户对于特定部门

+0

我用你的建议,它抛出了这个错误: **未知n在“Department”上记录属性/相关组件“用户”** –

+0

@CareyEstes是否重建过类(doctrine:build --all-classes)? – ManseUK

+0

是的,并清除缓存。我注意到我没有foreignAlias:用于部门关系的Schema中的用户(参见上面的模式)。这会抛出错误?从那以后,我加入了ForeignAlias:用户,但仍然没有运气。获取相同的未知记录属性错误 –