+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2014 |
+--------------------------------------------------------------------+
- CONST NOT_NULL = 1, IS_NULL = 2,
- DB_DAO_NOTNULL = 128,
- VALUE_SEPARATOR = "\ 1",
- BULK_INSERT_COUNT = 200,
- BULK_INSERT_HIGH_COUNT = 200,
- // special value for mail bulk inserts to avoid
- // potential duplication, assuming a smaller number reduces number of queries
- // by some factor, so some tradeoff. CRM-8678
- BULK_MAIL_INSERT_COUNT = 10,
- QUERY_FORMAT_WILDCARD = 1,
- QUERY_FORMAT_NO_QUOTES = 2;
-
- /*
+ const NOT_NULL = 1, IS_NULL = 2,
+ DB_DAO_NOTNULL = 128,
+ VALUE_SEPARATOR = "\ 1",
+ BULK_INSERT_COUNT = 200,
+ BULK_INSERT_HIGH_COUNT = 200,
+ // special value for mail bulk inserts to avoid
+ // potential duplication, assuming a smaller number reduces number of queries
+ // by some factor, so some tradeoff. CRM-8678
+ BULK_MAIL_INSERT_COUNT = 10,
+ QUERY_FORMAT_WILDCARD = 1,
+ QUERY_FORMAT_NO_QUOTES = 2;
+
+ /**
$options = &PEAR::getStaticProperty('DB_DataObject', 'options');
$options['database'] = $dsn;
if (defined('CIVICRM_DAO_DEBUG')) {
$options = &PEAR::getStaticProperty('DB_DataObject', 'options');
$options['database'] = $dsn;
if (defined('CIVICRM_DAO_DEBUG')) {
*/
protected function assignTestFK($fieldName, $fieldDef, $params) {
$required = CRM_Utils_Array::value('required', $fieldDef);
$FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
$dbName = $fieldDef['name'];
*/
protected function assignTestFK($fieldName, $fieldDef, $params) {
$required = CRM_Utils_Array::value('required', $fieldDef);
$FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
$dbName = $fieldDef['name'];
*/
protected function assignTestValue($fieldName, &$fieldDef, $counter) {
$dbName = $fieldDef['name'];
*/
protected function assignTestValue($fieldName, &$fieldDef, $counter) {
$dbName = $fieldDef['name'];
- * @param string $query the SQL query for execution
- * @param bool $i18nRewrite whether to rewrite the query
+ * @param string $query
+ * The SQL query for execution.
+ * @param bool $i18nRewrite
+ * Whether to rewrite the query.
// rewrite queries that should use $dbLocale-based views for multi-language installs
global $dbLocale;
if ($i18nRewrite and $dbLocale) {
// rewrite queries that should use $dbLocale-based views for multi-language installs
global $dbLocale;
if ($i18nRewrite and $dbLocale) {
static $sequenceKeys;
if (!isset($sequenceKeys)) {
$sequenceKeys = array('id', TRUE);
static $sequenceKeys;
if (!isset($sequenceKeys)) {
$sequenceKeys = array('id', TRUE);
$result = parent::delete($useWhere);
$event = new \Civi\Core\DAO\Event\PostDelete($this, $result);
$result = parent::delete($useWhere);
$event = new \Civi\Core\DAO\Event\PostDelete($this, $result);
- $dao = new CRM_Core_DAO_Log();
- $dao->entity_table = $this->getTableName();
- $dao->entity_id = $this->id;
- $dao->modified_id = $cid;
+ $dao = new CRM_Core_DAO_Log();
+ $dao->entity_table = $this->getTableName();
+ $dao->entity_id = $this->id;
+ $dao->modified_id = $cid;
* Given an associative array of name/value pairs, extract all the values
* that belong to this object and initialize the object with said values
*
* Given an associative array of name/value pairs, extract all the values
* that belong to this object and initialize the object with said values
*
* this is a destructive store, calling function is responsible
* for keeping sanity of id's.
*
* this is a destructive store, calling function is responsible
* for keeping sanity of id's.
*
- * @param object $object the object that we are extracting data from
- * @param array $values (reference ) associative array of name/value pairs
+ * @param object $object
+ * The object that we are extracting data from.
+ * @param array $values
+ * (reference ) associative array of name/value pairs.
$fields = &$object->fields();
foreach ($fields as $name => $value) {
$dbName = $value['name'];
$fields = &$object->fields();
foreach ($fields as $name => $value) {
$dbName = $value['name'];
if ($field) {
if (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_STRING) {
$maxLength = CRM_Utils_Array::value('maxlength', $field);
if ($field) {
if (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_STRING) {
$maxLength = CRM_Utils_Array::value('maxlength', $field);
* Get the size and maxLength attributes for this text field
* (or for all text fields) in the DAO object.
*
* Get the size and maxLength attributes for this text field
* (or for all text fields) in the DAO object.
*
- static function getAttribute($class, $fieldName = NULL) {
- $object = new $class( );
+ public static function getAttribute($class, $fieldName = NULL) {
+ $object = new $class();
$fields = &$object->fields();
if ($fieldName != NULL) {
$field = CRM_Utils_Array::value($fieldName, $fields);
$fields = &$object->fields();
if ($fieldName != NULL) {
$field = CRM_Utils_Array::value($fieldName, $fields);
CRM_Core_Error::fatal('This function is obsolete, please use CRM_Core_Transaction');
}
/**
* Check if there is a record with the same name in the db
*
CRM_Core_Error::fatal('This function is obsolete, please use CRM_Core_Transaction');
}
/**
* Check if there is a record with the same name in the db
*
- * @param string $value the value of the field we are checking
- * @param string $daoName the dao object name
- * @param string $daoID the id of the object being updated. u can change your name
+ * @param string $value
+ * The value of the field we are checking.
+ * @param string $daoName
+ * The dao object name.
+ * @param string $daoID
+ * The id of the object being updated. u can change your name.
- static function objectExists($value, $daoName, $daoID, $fieldName = 'name') {
- $object = new $daoName( );
+ public static function objectExists($value, $daoName, $daoID, $fieldName = 'name') {
+ $object = new $daoName();
- $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, $i18nRewrite);
+ $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, $i18nRewrite);
- static function getStorageValues($tableName = NULL, $maxTablesToCheck = 10, $fieldName = 'Engine') {
+ public static function getStorageValues($tableName = NULL, $maxTablesToCheck = 10, $fieldName = 'Engine') {
// show error if any of the tables, use 'MyISAM' storage engine.
$engines = self::getStorageValues(NULL, $maxTablesToCheck);
if (array_key_exists('MyISAM', $engines)) {
// show error if any of the tables, use 'MyISAM' storage engine.
$engines = self::getStorageValues(NULL, $maxTablesToCheck);
if (array_key_exists('MyISAM', $engines)) {
if (!array_key_exists($tableName, $show)) {
$query = "SHOW CREATE TABLE $tableName";
$dao = CRM_Core_DAO::executeQuery($query);
if (!array_key_exists($tableName, $show)) {
$query = "SHOW CREATE TABLE $tableName";
$dao = CRM_Core_DAO::executeQuery($query);
}
$constraint = "`FK_{$tableName}_{$columnName}`";
$pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
}
$constraint = "`FK_{$tableName}_{$columnName}`";
$pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
- static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
- $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
- $dao = CRM_Core_DAO::executeQuery($query);
+ public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
+ $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
+ $dao = CRM_Core_DAO::executeQuery($query);
- static function checkFieldIsAlwaysNull($tableName, $columnName) {
- $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
- $dao = CRM_Core_DAO::executeQuery($query);
+ public static function checkFieldIsAlwaysNull($tableName, $columnName) {
+ $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
+ $dao = CRM_Core_DAO::executeQuery($query);
/**
* Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
*
/**
* Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
*
- * @param string $daoName Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact)
- * @param int $searchValue Value of the column you want to search by
- * @param string $returnColumn Name of the column you want to GET the value of
- * @param string $searchColumn Name of the column you want to search by
- * @param boolean $force Skip use of the cache
+ * @param string $daoName
+ * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
+ * @param int $searchValue
+ * Value of the column you want to search by.
+ * @param string $returnColumn
+ * Name of the column you want to GET the value of.
+ * @param string $searchColumn
+ * Name of the column you want to search by.
+ * @param bool $force
+ * Skip use of the cache.
- static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
+ public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
/**
* Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
*
/**
* Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
*
- * @param string $daoName Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact)
- * @param int $searchValue Value of the column you want to search by
- * @param string $setColumn Name of the column you want to SET the value of
- * @param string $setValue SET the setColumn to this value
- * @param string $searchColumn Name of the column you want to search by
- *
- * @return boolean true if we found and updated the object, else false
+ * @param string $daoName
+ * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
+ * @param int $searchValue
+ * Value of the column you want to search by.
+ * @param string $setColumn
+ * Name of the column you want to SET the value of.
+ * @param string $setValue
+ * SET the setColumn to this value.
+ * @param string $searchColumn
+ * Name of the column you want to search by.
+ *
+ * @return boolean
+ * true if we found and updated the object, else false
- static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
- $object = new $daoName( );
+ public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
+ $object = new $daoName();
$object->selectAdd();
$object->selectAdd("$searchColumn, $setColumn");
$object->$searchColumn = $searchValue;
$object->selectAdd();
$object->selectAdd("$searchColumn, $setColumn");
$object->$searchColumn = $searchValue;
// check if sort is of type CRM_Utils_Sort
if (is_a($sort, 'CRM_Utils_Sort')) {
return $sort->orderBy();
// check if sort is of type CRM_Utils_Sort
if (is_a($sort, 'CRM_Utils_Sort')) {
return $sort->orderBy();
- * @param string $daoName name of the dao object
- * @param array $params (reference ) an assoc array of name/value pairs
- * @param array $defaults (reference ) an assoc array to hold the flattened values
- * @param array $returnProperities an assoc array of fields that need to be returned, eg array( 'first_name', 'last_name')
- *
- * @return object an object of type referenced by daoName
- * @access public
+ * @param string $daoName
+ * Name of the dao object.
+ * @param array $params
+ * (reference ) an assoc array of name/value pairs.
+ * @param array $defaults
+ * (reference ) an assoc array to hold the flattened values.
+ * @param array $returnProperities
+ * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
+ *
+ * @return object
+ * an object of type referenced by daoName
- static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
- $object = new $daoName( );
+ public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
+ $object = new $daoName();
- * @param string $daoName name of the dao object
- * @param int $contactId id of the contact to delete
+ * @param string $daoName
+ * Name of the dao object.
+ * @param int $contactId
+ * Id of the contact to delete.
- static function deleteEntityContact($daoName, $contactId) {
- $object = new $daoName( );
+ public static function deleteEntityContact($daoName, $contactId) {
+ $object = new $daoName();
- * @param string $daoName name of the dao
- * @param array $criteria array of all the fields & values
+ * @param string $daoName
+ * Name of the dao.
+ * @param array $criteria
+ * Array of all the fields & values.
- * @param string $fieldsFix array of fields that you want to prefix/suffix/replace
- * @param string $blockCopyOfDependencies fields that you want to block from
+ * @param string $fieldsFix
+ * Array of fields that you want to prefix/suffix/replace.
+ * @param string $blockCopyOfDependencies
+ * Fields that you want to block from.
*/
static function ©Generic($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
*/
static function ©Generic($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
- static function cascadeUpdate($daoName, $fromId, $toId, $newData = array()) {
- $object = new $daoName( );
+ public static function cascadeUpdate($daoName, $fromId, $toId, $newData = array()) {
+ $object = new $daoName();
- * @param array $returnProperities an assoc array of fields that need to be returned, eg array( 'first_name', 'last_name')
+ * @param array $returnProperities
+ * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
- static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
- require_once (str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php");
- $object = new $daoName( );
+ public static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
+ require_once(str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php");
+ $object = new $daoName();
if (!defined('CIVICRM_DSN')) {
if (function_exists('mysql_real_escape_string')) {
return mysql_real_escape_string($string);
if (!defined('CIVICRM_DSN')) {
if (function_exists('mysql_real_escape_string')) {
return mysql_real_escape_string($string);
- * @param $strings array
- * @param $default string the value to use if $strings has no elements
- * @return string eg "abc","def","ghi"
+ * @param array $strings
+ * @param string $default
+ * the value to use if $strings has no elements.
+ * @return string
+ * eg "abc","def","ghi"
// CRM-9155
// ensure we escape the single characters % and _ which are mysql wild
// card characters and could come in via sortByCharacter
// note that mysql does not escape these characters
if ($string && in_array($string,
array('%', '_', '%%', '_%')
// CRM-9155
// ensure we escape the single characters % and _ which are mysql wild
// card characters and could come in via sortByCharacter
// note that mysql does not escape these characters
if ($string && in_array($string,
array('%', '_', '%%', '_%')
+ // 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 (class_exists($baoName)) {
+ $daoName = $baoName;
+ }
+
for ($i = 0; $i < $numObjects; ++$i) {
++$counter;
/** @var CRM_Core_DAO $object */
$object = new $daoName();
for ($i = 0; $i < $numObjects; ++$i) {
++$counter;
/** @var CRM_Core_DAO $object */
$object = new $daoName();
foreach ($fields as $fieldName => $fieldDef) {
$dbName = $fieldDef['name'];
$FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
foreach ($fields as $fieldName => $fieldDef) {
$dbName = $fieldDef['name'];
$FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
if ($FKClassName != NULL) {
$object->assignTestFK($fieldName, $fieldDef, $params);
continue;
if ($FKClassName != NULL) {
$object->assignTestFK($fieldName, $fieldDef, $params);
continue;
//this is a test function also backtrace is set for the test suite it sometimes unsets itself
// so we re-set here in case
$config = CRM_Core_Config::singleton();
//this is a test function also backtrace is set for the test suite it sometimes unsets itself
// so we re-set here in case
$config = CRM_Core_Config::singleton();
&& ($required || $dbName == 'contact_id')
//I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
// to make this test process pass - line below makes pass for now
&& ($required || $dbName == 'contact_id')
//I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
// to make this test process pass - line below makes pass for now
foreach ($deletions as $deletion) {
CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]);
foreach ($deletions as $deletion) {
CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]);
- static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
+ public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
// test for create view and trigger permissions and if allowed, add the option to go multilingual
// and logging
// I'm not sure why we use the getStaticProperty for an error, rather than checking for DB_Error
// test for create view and trigger permissions and if allowed, add the option to go multilingual
// and logging
// I'm not sure why we use the getStaticProperty for an error, rather than checking for DB_Error
* * Stop using functions and find another way to strip numeric characters from phones
* * Give better error messages (currently a missing fn fatals with "unknown error")
*/
* * Stop using functions and find another way to strip numeric characters from phones
* * Give better error messages (currently a missing fn fatals with "unknown error")
*/
if (!self::$_checkedSqlFunctionsExist) {
self::$_checkedSqlFunctionsExist = TRUE;
$dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
if (!self::$_checkedSqlFunctionsExist) {
self::$_checkedSqlFunctionsExist = TRUE;
$dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
- * @param $info array per hook_civicrm_triggerInfo
- * @param $onlyTableName string the specific table requiring a rebuild; or NULL to rebuild all tables
+ * @param array $info
+ * per hook_civicrm_triggerInfo.
+ * @param string $onlyTableName
+ * the specific table requiring a rebuild; or NULL to rebuild all tables.
// Validate info array, should probably raise errors?
if (is_array($info) == FALSE) {
return;
// Validate info array, should probably raise errors?
if (is_array($info) == FALSE) {
return;
- $varString = implode("\n", $parts['variables']);
- $sqlString = implode("\n", $parts['sql']);
- $validName = CRM_Core_DAO::shortenSQLName($tableName, 48, TRUE);
+ $varString = implode("\n", $parts['variables']);
+ $sqlString = implode("\n", $parts['sql']);
+ $validName = CRM_Core_DAO::shortenSQLName($tableName, 48, TRUE);
- * @return array each item has keys:
- * - name: string
- * - type: string
- * - count: int
- * - table: string|null SQL table name
- * - key: string|null SQL column name
- */
- function getReferenceCounts() {
+ * @return array
+ * each item has keys:
+ * - name: string
+ * - type: string
+ * - count: int
+ * - table: string|null SQL table name
+ * - key: string|null SQL column name
+ */
+ public function getReferenceCounts() {
* schema metadata in dynamicForeignKey which enumerates a restricted
* set of possible entity_table's.
*
* schema metadata in dynamicForeignKey which enumerates a restricted
* set of possible entity_table's.
*
- * @return array structure of table and column, listing every table with a
- * foreign key reference to $tableName, and the column where the key appears.
+ * @return array
+ * structure of table and column, listing every table with a
+ * foreign key reference to $tableName, and the column where the key appears.
$refsFound = array();
foreach (CRM_Core_DAO_AllCoreTables::getClasses() as $daoClassName) {
$links = $daoClassName::getReferenceColumns();
$refsFound = array();
foreach (CRM_Core_DAO_AllCoreTables::getClasses() as $daoClassName) {
$links = $daoClassName::getReferenceColumns();
* The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
*
* @param string $fieldName
* The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
*
* @param string $fieldName
- * @param string $context : @see CRM_Core_DAO::buildOptionsContext
- * @param array $props : whatever is known about this bao object
+ * @param string $context
+ * @see CRM_Core_DAO::buildOptionsContext.
+ * @param array $props
+ * whatever is known about this bao object.
- * @param string $fieldName name of fields
- * @param $filter array filter to be applied indexed by operator
- * @param $type String type of field (not actually used - nor in api @todo )
- * @param $alias String alternative field name ('as') @todo- not actually used
- * @param bool $returnSanitisedArray return a sanitised array instead of a clause
- * this is primarily so we can add filters @ the api level to the Query object based fields
+ * @param string $fieldName
+ * Name of fields.
+ * @param array $filter
+ * filter to be applied indexed by operator.
+ * @param string $type
+ * type of field (not actually used - nor in api @todo ).
+ * @param string $alias
+ * alternative field name ('as') @todo- not actually used.
+ * @param bool $returnSanitisedArray
+ * Return a sanitised array instead of a clause.
+ * this is primarily so we can add filters @ the api level to the Query object based fields
* preferable to not double-banger the return context next refactor of this - but keeping the attention
* in one place has some advantages as we try to extend this format
*
* preferable to not double-banger the return context next refactor of this - but keeping the attention
* in one place has some advantages as we try to extend this format
*
- * @return NULL|string|array a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
+ * @return NULL|string|array
+ * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
* depending on whether it is supported as yet
*/
public static function createSQLFilter($fieldName, $filter, $type, $alias = NULL, $returnSanitisedArray = FALSE) {
* depending on whether it is supported as yet
*/
public static function createSQLFilter($fieldName, $filter, $type, $alias = NULL, $returnSanitisedArray = FALSE) {
- return(sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria)));
+ return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria)));
- return array('=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=", "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'IS NOT NULL', 'IS NULL');
+ return array(
+ '=',
+ '<=',
+ '>=',
+ '>',
+ '<',
+ 'LIKE',
+ "<>",
+ "!=",
+ "NOT LIKE",
+ 'IN',
+ 'NOT IN',
+ 'BETWEEN',
+ 'NOT BETWEEN',
+ 'IS NOT NULL',
+ 'IS NULL'
+ );
* which can be longer than this length, this function helps with creating
* strings that meet various criteria.
*
* which can be longer than this length, this function helps with creating
* strings that meet various criteria.
*
- * @param string $string - the string to be shortened
- * @param int $length - the max length of the string
+ * @param string $string
+ * The string to be shortened.
+ * @param int $length
+ * The max length of the string.