From ffcef05476019877c04d883352f8c599ef7aa758 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 21 May 2014 00:32:01 -0700 Subject: [PATCH] CRM-14478 - Add CRM_Core_Reference_OptionValue --- CRM/Core/DAO.php | 33 ++++++++++++++++++++++--- CRM/Core/Reference/Interface.php | 2 +- CRM/Core/Reference/OptionValue.php | 39 ++++++++++++++++++++++++++++++ xml/templates/dao.tpl | 7 +++--- 4 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 CRM/Core/Reference/OptionValue.php diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index d4c00fa386..cceb90211f 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -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 + */ + 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; + } } } diff --git a/CRM/Core/Reference/Interface.php b/CRM/Core/Reference/Interface.php index 25148d34e3..f949994c3b 100644 --- a/CRM/Core/Reference/Interface.php +++ b/CRM/Core/Reference/Interface.php @@ -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 index 0000000000..7c97972afc --- /dev/null +++ b/CRM/Core/Reference/OptionValue.php @@ -0,0 +1,39 @@ +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 diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index b42c755da9..be5768e58a 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -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} -- 2.25.1