繁体   English   中英

在带有symfony的循环中使用外键

[英]Using a foreign key in a loop with 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

到目前为止,我的作品仍然有效,但是我认为它可以更好地执行。 这就是我的工作原理:

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

并在索引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; ?>

现在,这可行,但是如果部门列表过长该怎么办。 这意味着我必须在操作中进行新的数据库查询,并在indexSuccess.php文件中使用foreach选项。

有没有一种方法可以在行中的后续数据上使用WHERE选项提取所有部门数据?

更新

这是架构,我认为它与@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 }

没有看到您的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() -返回特定部门的所有用户

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM