我有三個表。兩個使用一個外鍵來提取數據,一個顯示數據。在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 }
我用你的建議,它拋出了這個錯誤: **未知n在「Department」上記錄屬性/相關組件「用戶」** –
@CareyEstes是否重建過類(doctrine:build --all-classes)? – ManseUK
是的,並清除緩存。我注意到我沒有foreignAlias:用於部門關係的Schema中的用戶(參見上面的模式)。這會拋出錯誤?從那以後,我加入了ForeignAlias:用戶,但仍然沒有運氣。獲取相同的未知記錄屬性錯誤 –