$occurrences = array();
foreach ($links as $refSpec) {
- $refColumn = $refSpec->getReferenceKey();
- $targetColumn = $refSpec->getTargetKey();
- $params = array(1 => array($this->$targetColumn, 'String'));
- $sql = <<<EOS
-SELECT id
-FROM {$refSpec->getReferenceTable()}
-WHERE {$refColumn} = %1
-EOS;
- if ($refSpec->isGeneric()) {
- $params[2] = array(static::getTableName(), 'String');
- $sql .= <<<EOS
- AND {$refSpec->getTypeColumn()} = %2
-EOS;
- }
+ /** @var $refSpec CRM_Core_EntityReference */
$daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($refSpec->getReferenceTable());
- $result = self::executeQuery($sql, $params, TRUE, $daoName);
+ $result = $refSpec->findReferences($this);
while ($result->fetch()) {
$obj = new $daoName();
$obj->id = $result->id;
function isGeneric() {
return ($this->refTypeColumn !== NULL);
}
+
+ /**
+ * 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)
+ */
+ public function findReferences($targetDao) {
+ $refColumn = $this->getReferenceKey();
+ $targetColumn = $this->getTargetKey();
+ $params = array(1 => array($targetDao->$targetColumn, 'String'));
+ $sql = <<<EOS
+SELECT id
+FROM {$this->getReferenceTable()}
+WHERE {$refColumn} = %1
+EOS;
+ if ($this->isGeneric()) {
+ // If anyone complains about $dao::getTableName(), then could use
+ // "$daoClass=get_class($dao); $daoClass::getTableName();"
+ $params[2] = array($targetDao::getTableName(), 'String');
+ $sql .= <<<EOS
+ AND {$this->getTypeColumn()} = %2
+EOS;
+ }
+ $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable());
+ $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName);
+ return $result;
+ }
}