学说:超越男人对多人的左撇子条件

时间:2012-03-27 10:32:34

标签: symfony1 doctrine doctrine-1.2

假设我有一个架构,其中用户有许多警报(多对多)。

Alert:
  columns: ~

User:
  columns: ~
  relations:
    Alerts:
      class: Alert
      refClass: UserAlert
      local: user_id
      foreign: alert_id

UserAlert:
  columns:
    user_id:
      type: integer(4)
      primary: true
    alert_id:
      type: integer(4)
      primary: true
    active:
      type: boolean
      notnull: true
      default: true
  relations:
    Alert:
      local: alert_id
      foreign: id
    User:
      local: user_id
      foreign: id

请注意ref类中的自定义active字段,该字段指示相关警报是否对用户有效。如何通过活动警报获取用户?

更新: 实现此目的的一种方法是使用WITH关键字覆盖连接条件:

$user = UserTable::createQuery('u')
      ->createQuery('u')
      ->leftJoin('u.UserAlert aa WITH aa.enabled = ?', array(true))
      ->leftJoin('aa.Alert a')
      ->addWhere('u.id = ?', $id)
      ->fetchOne();

但是这样你就不得不调用$user->getUserAlert()来返回一个ref类对象的集合。我希望填充“警报”关系,以便我可以直接致电$user->getAlerts()

1 个答案:

答案 0 :(得分:1)

首先,您的架构是错误的(至少,它不适合我)。这是正确的(我在用户和警报中放置了一些字段用于测试):

Alert:
  columns:
    name: string(30)
  relations:
    Users:
      class: User
      local: alert_id
      foreign: user_id
      refClass: UserAlert
      foreignAlias: Alerts

User:
  columns:
    name: string(255)
  relations:
    Alerts:
      class: Alert
      local: user_id
      foreign: alert_id
      refClass: UserAlert
      foreignAlias: Users

UserAlert:
  columns:
    user_id:
      type: integer
      primary: true
    alert_id:
      type: integer
      primary: true
    active:
      type: boolean
  relations:
    User:
      class: User
      local: user_id
    Alert:
      class: Alert
      local: alert_id

您必须在Alert,User和UserAlert中定义many2many实现。

然后我构建数据库并在其中放入一些数据,用于测试:http://sqlfiddle.com/#!2/dd507/2

现在,要检索只有活动警报的所有用户,请将其放入UserTable.php

public function retrieveAlerts($active = true)
{
  $q = $this->createQuery('u')
    ->leftJoin('u.UserAlert ua')
    ->leftJoin('ua.Alert a')
    ->where('ua.active = ?', array($active));

  return $q->fetchArray();
}

然后,在你的行动中,只需这样做:

$users = Doctrine_Core::getTable('User')->retrieveAlerts();

如果您print_r结果,我们会看到我们通过其有效提醒检索用户34

Array
(
  [0] => Array
    (
      [id] => 3
      [name] => tata
      [UserAlert] => Array
        (
          [0] => Array
            (
              [user_id] => 3
              [alert_id] => 1
              [active] => 1
              [Alert] => Array
                (
                  [id] => 1
                  [name] => new
                )
            )
          [1] => Array
            (
              [user_id] => 3
              [alert_id] => 2
              [active] => 1
              [Alert] => Array
                (
                  [id] => 2
                  [name] => fired
                )
            )
        )
    )
  [1] => Array
    (
      [id] => 4
      [name] => tutu
      [UserAlert] => Array
        (
          [0] => Array
            (
              [user_id] => 4
              [alert_id] => 3
              [active] => 1
              [Alert] => Array
                (
                  [id] => 3
                  [name] => sold
                )
            )
        )
    )
)