CRM_Core_DAO::fetchMap() - Add helper to return key-value pairs from SQL
authorTim Otten <totten@civicrm.org>
Wed, 14 Sep 2016 03:21:47 +0000 (20:21 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 14 Sep 2016 03:21:47 +0000 (20:21 -0700)
CRM/Core/DAO.php

index 4747adee2fab323a4c3e4c693ce800bce0c87bbd..7836d9dde20e7ded2cb94ec1338f620ea94be4e1 100644 (file)
@@ -1008,6 +1008,24 @@ FROM   civicrm_domain
     return $result;
   }
 
+  /**
+   * Get all the result records as mapping between columns.
+   *
+   * @param string $keyColumn
+   *   Ex: "name"
+   * @param string $valueColumn
+   *   Ex: "label"
+   * @return array
+   *   Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
+   */
+  public function fetchMap($keyColumn, $valueColumn) {
+    $result = array();
+    while ($this->fetch()) {
+      $result[$this->{$keyColumn}] = $this->{$valueColumn};
+    }
+    return $result;
+  }
+
   /**
    * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
    *