CRM-14478 - Add CRM_Core_Reference_OptionValue
authorTim Otten <totten@civicrm.org>
Wed, 21 May 2014 07:32:01 +0000 (00:32 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 23 May 2014 03:40:25 +0000 (20:40 -0700)
CRM/Core/DAO.php
CRM/Core/Reference/Interface.php
CRM/Core/Reference/OptionValue.php [new file with mode: 0644]
xml/templates/dao.tpl

index d4c00fa3865289f801f05afa8fddefee3789d76b..cceb90211f835bc19738c489ee2816457b7d30c4 100644 (file)
@@ -1731,6 +1731,29 @@ SELECT contact_id
     }
   }
 
+  /**
+   * Given a list of fields, create a list of references.
+   *
+   * @param string $className BAO/DAO class name
+   * @return array<CRM_Core_Reference_Interface>
+   */
+  static function createReferenceColumns($className) {
+    $result = array();
+    $fields = $className::fields();
+    foreach ($fields as $field) {
+      if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
+        $result[] = new CRM_Core_Reference_OptionValue(
+          $className::getTableName(),
+          $field['name'],
+          'civicrm_option_value',
+          CRM_Utils_Array::value('keyColumn', $field['pseudoconstant'], 'value'),
+          $field['pseudoconstant']['optionGroupName']
+        );
+      }
+    }
+    return $result;
+  }
+
   /**
    * Find all records which refer to this entity.
    *
@@ -1744,10 +1767,12 @@ SELECT contact_id
       /** @var $refSpec CRM_Core_Reference_Interface */
       $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($refSpec->getReferenceTable());
       $result = $refSpec->findReferences($this);
-      while ($result->fetch()) {
-        $obj = new $daoName();
-        $obj->id = $result->id;
-        $occurrences[] = $obj;
+      if ($result) {
+        while ($result->fetch()) {
+          $obj = new $daoName();
+          $obj->id = $result->id;
+          $occurrences[] = $obj;
+        }
       }
     }
 
index 25148d34e39648aa3d2e2fd7f517e6f50ce07ba7..f949994c3b11b82d9bbf0233f895b343f8c79962 100644 (file)
@@ -16,7 +16,7 @@ interface CRM_Core_Reference_Interface {
    * Create a query to find references to a particular record
    *
    * @param CRM_Core_DAO $targetDao the instance for which we want references
-   * @return CRM_Core_DAO a query-handle (like the result of CRM_Core_DAO::executeQuery)
+   * @return CRM_Core_DAO|NULL a query-handle (like the result of CRM_Core_DAO::executeQuery)
    */
   public function findReferences($targetDao);
 }
diff --git a/CRM/Core/Reference/OptionValue.php b/CRM/Core/Reference/OptionValue.php
new file mode 100644 (file)
index 0000000..7c97972
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * Description of a one-way link between an option-value and an entity
+ */
+class CRM_Core_Reference_OptionValue extends CRM_Core_Reference_Basic {
+  /**
+   * @var string option-group-name
+   */
+  protected $targetOptionGroupName;
+
+  /**
+   * @var int|NULL null if not yet loaded
+   */
+  protected $targetOptionGroupId;
+
+  function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $optionGroupName) {
+    parent::__construct($refTable, $refKey, $targetTable, $targetKey, NULL);
+    $this->targetOptionGroupName = $optionGroupName;
+  }
+
+  public function findReferences($targetDao) {
+    if (! ($targetDao instanceof CRM_Core_DAO_OptionValue)) {
+      throw new CRM_Core_Exception("Mismatched reference: expected OptionValue but received " . get_class($targetDao));
+    }
+    if ($targetDao->option_group_id == $this->getTargetOptionGroupId()) {
+      return parent::findReferences($targetDao);
+    } else {
+      return NULL;
+    }
+  }
+
+  public function getTargetOptionGroupId() {
+    if ($this->targetOptionGroupId === NULL) {
+      $this->targetOptionGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->targetOptionGroupName, 'id', 'name');
+    }
+    return $this->targetOptionGroupId;
+  }
+}
\ No newline at end of file
index b42c755da929da91fbc5db96db24f03cad92561a..be5768e58a310e52faa7f1321b784ba614db14d3 100644 (file)
@@ -141,15 +141,14 @@ class {$table.className} extends CRM_Core_DAO {ldelim}
      */
     static function getReferenceColumns() {ldelim}
       if (!self::$_links) {ldelim}
-        self::$_links = array(
+        self::$_links = static::createReferenceColumns(__CLASS__);
 {foreach from=$table.foreignKey item=foreign}
-          new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'),
+        self::$_links[] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
 {/foreach}
 
 {foreach from=$table.dynamicForeignKey item=foreign}
-          new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'),
+        self::$_links[] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
 {/foreach}
-        );
       {rdelim}
       return self::$_links;
     {rdelim}