+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019 |
+ | Copyright CiviCRM LLC (c) 2004-2020 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
* All DAO classes should inherit from this class.
*
* @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC (c) 2004-2020
*/
if (!defined('DB_DSN_MODE')) {
$event = new \Civi\Core\DAO\Event\PostUpdate($this);
\Civi::service('dispatcher')->dispatch("civi.dao.postUpdate", $event);
}
+ $this->clearDbColumnValueCache();
}
else {
$this->insert();
\Civi::service('dispatcher')->dispatch("civi.dao.postDelete", $event);
$this->free();
+ $this->clearDbColumnValueCache();
+
return $result;
}
return $result;
}
+ /**
+ * Check if a given table has data.
+ *
+ * @param string $tableName
+ * @return bool
+ * TRUE if $tableName has at least one record.
+ */
+ public static function checkTableHasData($tableName) {
+ $c = CRM_Core_DAO::singleValueQuery(sprintf('SELECT count(*) c FROM `%s`', $tableName));
+ return $c > 0;
+ }
+
/**
* @param $version
*
CRM_Core_Error::fatal();
}
- $cacheKey = "{$daoName}:{$searchValue}:{$returnColumn}:{$searchColumn}";
- if (self::$_dbColumnValueCache === NULL) {
- self::$_dbColumnValueCache = [];
+ self::$_dbColumnValueCache = self::$_dbColumnValueCache ?? [];
+
+ while (strpos($daoName, '_BAO_') !== FALSE) {
+ $daoName = get_parent_class($daoName);
}
- if (!array_key_exists($cacheKey, self::$_dbColumnValueCache) || $force) {
+ if ($force ||
+ empty(self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) ||
+ !array_key_exists($returnColumn, self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue])
+ ) {
$object = new $daoName();
$object->$searchColumn = $searchValue;
$object->selectAdd();
if ($object->find(TRUE)) {
$result = $object->$returnColumn;
}
- $object->free();
- self::$_dbColumnValueCache[$cacheKey] = $result;
+ self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn] = $result;
}
- return self::$_dbColumnValueCache[$cacheKey];
+ return self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn];
}
/**
$tr['%' . $key] = $item[0];
}
elseif ($abort) {
- CRM_Core_Error::fatal("{$item[0]} is not of type {$item[1]}");
+ throw new CRM_Core_Exception("{$item[0]} is not of type {$item[1]}");
}
}
}
/**
* Drop all CiviCRM tables.
*
- * @throws \CRM_Exception
+ * @throws \CRM_Core_Exception
*/
public static function dropAllTables() {
// Prefer to instantiate BAO's instead of DAO's (when possible)
// so that assignTestValue()/assignTestFK() can be overloaded.
$baoName = str_replace('_DAO_', '_BAO_', $daoName);
+ if ($baoName === 'CRM_Financial_BAO_FinancialTrxn') {
+ // OMG OMG OMG this is so incredibly bad. The BAO is insanely named.
+ // @todo create a new class called what the BAO SHOULD be
+ // that extends BAO-crazy-name.... migrate.
+ $baoName = 'CRM_Core_BAO_FinancialTrxn';
+ }
if (class_exists($baoName)) {
$daoName = $baoName;
}
return $fields;
}
+ /**
+ * Remove item from static cache during update/delete operations
+ */
+ private function clearDbColumnValueCache() {
+ $daoName = get_class($this);
+ while (strpos($daoName, '_BAO_') !== FALSE) {
+ $daoName = get_parent_class($daoName);
+ }
+ if (isset($this->id)) {
+ unset(self::$_dbColumnValueCache[$daoName]['id'][$this->id]);
+ }
+ if (isset($this->name)) {
+ unset(self::$_dbColumnValueCache[$daoName]['name'][$this->name]);
+ }
+ }
+
}