From 11626cf1e79a782a0b677bfaacec5754d44868f2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 20 May 2014 23:06:39 -0700 Subject: [PATCH] CRM-14478 - Split CRM_Core_EntityReference into different classes --- CRM/Core/DAO.php | 9 ++-- .../Basic.php} | 33 ++++---------- CRM/Core/Reference/Dynamic.php | 43 +++++++++++++++++++ CRM/Core/Reference/Interface.php | 22 ++++++++++ tests/phpunit/CRM/Core/DAOTest.php | 4 +- xml/templates/dao.tpl | 6 +-- 6 files changed, 83 insertions(+), 34 deletions(-) rename CRM/Core/{EntityReference.php => Reference/Basic.php} (55%) create mode 100644 CRM/Core/Reference/Dynamic.php create mode 100644 CRM/Core/Reference/Interface.php diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index f786e089e8..d4c00fa386 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -238,7 +238,7 @@ class CRM_Core_DAO extends DB_DataObject { * @static * @access public * - * @return array of CRM_Core_EntityReference + * @return array of CRM_Core_Reference_Interface */ static function getReferenceColumns() { return array(); @@ -1741,7 +1741,7 @@ SELECT contact_id $occurrences = array(); foreach ($links as $refSpec) { - /** @var $refSpec CRM_Core_EntityReference */ + /** @var $refSpec CRM_Core_Reference_Interface */ $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($refSpec->getReferenceTable()); $result = $refSpec->findReferences($this); while ($result->fetch()) { @@ -1775,9 +1775,8 @@ SELECT contact_id $daoTableName = $daoClassName::getTableName(); foreach ($links as $refSpec) { - if ($refSpec->getTargetTable() === $tableName - or $refSpec->isGeneric() - ) { + /** @var $refSpec CRM_Core_Reference_Interface */ + if ($refSpec->matchesTargetTable($tableName)) { $refsFound[] = $refSpec; } } diff --git a/CRM/Core/EntityReference.php b/CRM/Core/Reference/Basic.php similarity index 55% rename from CRM/Core/EntityReference.php rename to CRM/Core/Reference/Basic.php index 38b7890682..f72a664bd7 100644 --- a/CRM/Core/EntityReference.php +++ b/CRM/Core/Reference/Basic.php @@ -3,9 +3,9 @@ /** * Description of a one-way link between two entities * - * This could be a foreign key or a generic (entity_id, entity_table) pointer + * This is a basic SQL foreign key. */ -class CRM_Core_EntityReference { +class CRM_Core_Reference_Basic implements CRM_Core_Reference_Interface { protected $refTable; protected $refKey; protected $refTypeColumn; @@ -40,36 +40,21 @@ class CRM_Core_EntityReference { return $this->targetKey; } - /** - * @return true if the reference can point to more than one type - */ - function isGeneric() { - return ($this->refTypeColumn !== NULL); + public function matchesTargetTable($tableName) { + return ($this->getTargetTable() === $tableName); } - /** - * 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')); + $params = array( + 1 => array($targetDao->$targetColumn, 'String') + ); $sql = <<getReferenceTable()} -WHERE {$refColumn} = %1 +WHERE {$this->getReferenceKey()} = %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 .= <<getTypeColumn()} = %2 -EOS; - } + $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable()); $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName); return $result; diff --git a/CRM/Core/Reference/Dynamic.php b/CRM/Core/Reference/Dynamic.php new file mode 100644 index 0000000000..fdcf77546f --- /dev/null +++ b/CRM/Core/Reference/Dynamic.php @@ -0,0 +1,43 @@ +getReferenceKey(); + $targetColumn = $this->getTargetKey(); + + $params = array( + 1 => array($targetDao->$targetColumn, 'String'), + + // If anyone complains about $targetDao::getTableName(), then could use + // "{get_class($targetDao)}::getTableName();" + 2 => array($targetDao::getTableName(), 'String'), + ); + + $sql = <<getReferenceTable()} +WHERE {$refColumn} = %1 +AND {$this->getTypeColumn()} = %2 +EOS; + + $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($this->getReferenceTable()); + $result = CRM_Core_DAO::executeQuery($sql, $params, TRUE, $daoName); + return $result; + } +} diff --git a/CRM/Core/Reference/Interface.php b/CRM/Core/Reference/Interface.php new file mode 100644 index 0000000000..25148d34e3 --- /dev/null +++ b/CRM/Core/Reference/Interface.php @@ -0,0 +1,22 @@ +assertEquals('contact_id', $contactRef->getReferenceKey()); $this->assertEquals('id', $contactRef->getTargetKey()); - $this->assertEquals(FALSE, $contactRef->isGeneric()); + $this->assertEquals('CRM_Core_Reference_Basic', get_class($contactRef)); } function testGetReferencesToTable() { @@ -35,7 +35,7 @@ class CRM_Core_DAOTest extends CiviUnitTestCase { $this->assertEquals('entity_id', $genericRef->getReferenceKey()); $this->assertEquals('entity_table', $genericRef->getTypeColumn()); $this->assertEquals('id', $genericRef->getTargetKey()); - $this->assertEquals(TRUE, $genericRef->isGeneric()); + $this->assertEquals('CRM_Core_Reference_Dynamic', get_class($genericRef)); } function testFindReferences() { diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index be1b194a5b..b42c755da9 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -137,17 +137,17 @@ class {$table.className} extends CRM_Core_DAO {ldelim} * * @static * @access public - * @return array of CRM_Core_EntityReference + * @return array of CRM_Core_Reference_Interface */ static function getReferenceColumns() {ldelim} if (!self::$_links) {ldelim} self::$_links = array( {foreach from=$table.foreignKey item=foreign} - new CRM_Core_EntityReference(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'), + new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'), {/foreach} {foreach from=$table.dynamicForeignKey item=foreign} - new CRM_Core_EntityReference(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'), + new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'), {/foreach} ); {rdelim} -- 2.25.1