}
}
+ /**
+ * 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.
*
/** @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;
+ }
}
}
* 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);
}
--- /dev/null
+<?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
*/
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}