Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7069321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:25:53+00:00 2026-05-28T05:25:53+00:00

I have three tables. Two use a foreign key that pulls in data and

  • 0

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 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:25:54+00:00Added an answer on May 28, 2026 at 5:25 am

    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

    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
    

    The foreignAlias param 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables one has a three column composite key. The other needs
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have a database that has two tables, one of which contains a foreign
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have two product tables Product1 and Product2. There is a one 2 one
Lets say I have two tables, one for transactions, and another table who's primary
Say I have three tables, A, B and C. Conceptually A (optionally) has one
I have two tables. One is a 'Users' table. Each row (user) in the
We have two tables in a SQL Server 2005 database, A and B.There is
I have two tables Plants and Information. For every plant there are many information,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.