Merge pull request #14455 from civicrm/5.14
[civicrm-core.git] / CRM / Core / Reference / Basic.php
index 0b13ef5f5b551e5f234948667e6c88f20e5a806f..a3e1fe73f8a717ca9423c0fdfebfd200c5c7cca9 100644 (file)
@@ -19,7 +19,7 @@ class CRM_Core_Reference_Basic implements CRM_Core_Reference_Interface {
    * @param string $targetKey
    * @param null $refTypeColumn
    */
-  function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $refTypeColumn = NULL) {
+  public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $refTypeColumn = NULL) {
     $this->refTable = $refTable;
     $this->refKey = $refKey;
     $this->targetTable = $targetTable;
@@ -30,35 +30,35 @@ class CRM_Core_Reference_Basic implements CRM_Core_Reference_Interface {
   /**
    * @return mixed
    */
-  function getReferenceTable() {
+  public function getReferenceTable() {
     return $this->refTable;
   }
 
   /**
    * @return mixed
    */
-  function getReferenceKey() {
+  public function getReferenceKey() {
     return $this->refKey;
   }
 
   /**
    * @return null
    */
-  function getTypeColumn() {
+  public function getTypeColumn() {
     return $this->refTypeColumn;
   }
 
   /**
    * @return null
    */
-  function getTargetTable() {
+  public function getTargetTable() {
     return $this->targetTable;
   }
 
   /**
    * @return string
    */
-  function getTargetKey() {
+  public function getTargetKey() {
     return $this->targetKey;
   }
 
@@ -78,11 +78,16 @@ class CRM_Core_Reference_Basic implements CRM_Core_Reference_Interface {
    */
   public function findReferences($targetDao) {
     $targetColumn = $this->getTargetKey();
-    $params = array(
-      1 => array($targetDao->$targetColumn, 'String')
-    );
+    $select = 'id';
+    // CRM-19385: Since id is removed, return all rows for cache tables.
+    if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists($this->getReferenceTable(), 'id')) {
+      $select = '*';
+    }
+    $params = [
+      1 => [$targetDao->$targetColumn, 'String'],
+    ];
     $sql = <<<EOS
-SELECT id
+SELECT {$select}
 FROM {$this->getReferenceTable()}
 WHERE {$this->getReferenceKey()} = %1
 EOS;
@@ -99,21 +104,22 @@ EOS;
    */
   public function getReferenceCount($targetDao) {
     $targetColumn = $this->getTargetKey();
-    $params = array(
-      1 => array($targetDao->$targetColumn, 'String')
-    );
+    $params = [
+      1 => [$targetDao->$targetColumn, 'String'],
+    ];
     $sql = <<<EOS
-SELECT count(id)
+SELECT count(*)
 FROM {$this->getReferenceTable()}
 WHERE {$this->getReferenceKey()} = %1
 EOS;
 
-    return array(
-      'name' => implode(':', array('sql', $this->getReferenceTable(), $this->getReferenceKey())),
+    return [
+      'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
       'type' => get_class($this),
       'table' => $this->getReferenceTable(),
       'key' => $this->getReferenceKey(),
-      'count' => CRM_Core_DAO::singleValueQuery($sql, $params)
-    );
+      'count' => CRM_Core_DAO::singleValueQuery($sql, $params),
+    ];
   }
+
 }