[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/storage/lisk/ -> LiskDAOSet.php (source)

   1  <?php
   2  
   3  /**
   4   * You usually don't need to use this class directly as it is controlled by
   5   * @{class:LiskDAO}. You can create it if you want to work with objects of same
   6   * type from different sources as with one set. Let's say you want to get
   7   * e-mails of all users involved in a revision:
   8   *
   9   *   $users = new LiskDAOSet();
  10   *   $users->addToSet($author);
  11   *   foreach ($reviewers as $reviewer) {
  12   *     $users->addToSet($reviewer);
  13   *   }
  14   *   foreach ($ccs as $cc) {
  15   *     $users->addToSet($cc);
  16   *   }
  17   *   // Preload e-mails of all involved users and return e-mails of author.
  18   *   $author_emails = $author->loadRelatives(
  19   *     new PhabricatorUserEmail(),
  20   *     'userPHID',
  21   *     'getPHID');
  22   */
  23  final class LiskDAOSet {
  24    private $daos = array();
  25    private $relatives = array();
  26    private $subsets = array();
  27  
  28    public function addToSet(LiskDAO $dao) {
  29      if ($this->relatives) {
  30        throw new Exception("Don't call addToSet() after loading data!");
  31      }
  32      $this->daos[] = $dao;
  33      $dao->putInSet($this);
  34      return $this;
  35    }
  36  
  37    /**
  38     * The main purpose of this method is to break cyclic dependency.
  39     * It removes all objects from this set and all subsets created by it.
  40     */
  41    final public function clearSet() {
  42      $this->daos = array();
  43      $this->relatives = array();
  44      foreach ($this->subsets as $set) {
  45        $set->clearSet();
  46      }
  47      $this->subsets = array();
  48      return $this;
  49    }
  50  
  51  
  52    /**
  53     * See @{method:LiskDAO::loadRelatives}.
  54     */
  55    public function loadRelatives(
  56      LiskDAO $object,
  57      $foreign_column,
  58      $key_method = 'getID',
  59      $where = '') {
  60  
  61      $relatives = &$this->relatives[
  62        get_class($object)."-{$foreign_column}-{$key_method}-{$where}"];
  63  
  64      if ($relatives === null) {
  65        $ids = array();
  66        foreach ($this->daos as $dao) {
  67          $id = $dao->$key_method();
  68          if ($id !== null) {
  69            $ids[$id] = $id;
  70          }
  71        }
  72        if (!$ids) {
  73          $relatives = array();
  74        } else {
  75          $set = new LiskDAOSet();
  76          $this->subsets[] = $set;
  77          $relatives = $object->putInSet($set)->loadAllWhere(
  78            '%C IN (%Ls) %Q',
  79            $foreign_column,
  80            $ids,
  81            ($where != '' ? 'AND '.$where : ''));
  82          $relatives = mgroup($relatives, 'get'.$foreign_column);
  83        }
  84      }
  85  
  86      return $relatives;
  87    }
  88  
  89  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1