I have three tables. Two use a foreign key that pulls in data and one that displays the data.
My tables are like this:
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....
So I want to display the data in an order according to department with the type and user data in the row.
For instance:
Illustration
ink Fred Flintstone
charcoal Fred Flintstone
painting Fred Flintstone
Photography
ink Barney Rubble
painting Barney Rubble
What I have so far works, but I think it can be executed much better. This is whatI have that works:
apps/frontend/modules/foo/actions/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();
}
And in the index
apps/frontend/modules/foo/templates/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; ?>
Now, this works, but what if the department list gets really long. That means I have to make a new database query in the actions and use the foreach option in the indexSuccess.php file.
Is there a way to pull all of the department data with a WHERE option on the subsequent data in the row?
UPDATE
Here is the schema, which I think is pretty close to what @ManseUK suggested:
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 }
Without seeing your schema.yml its difficult to say – but if you have setup the aliases within the schema correctly then you should be able to do something like :
actions.php
indexSuccess.php
schema.yml
The
foreignAliasparam is the key here – it specifics the relationship name between tables – allowing you to call$dept->getUsers()– which returns all users for a specific department