3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Base Database Access Object class.
31 * All DAO classes should inherit from this class.
34 * @copyright CiviCRM LLC (c) 2004-2019
37 if (!defined('DB_DSN_MODE')) {
38 define('DB_DSN_MODE', 'auto');
41 require_once 'PEAR.php';
42 require_once 'DB/DataObject.php';
44 require_once 'CRM/Core/I18n.php';
49 class CRM_Core_DAO
extends DB_DataObject
{
52 * How many times has this instance been cloned.
56 protected $resultCopies = 0;
62 static $_nullObject = NULL;
67 static $_nullArray = array();
69 static $_dbColumnValueCache = NULL;
70 const NOT_NULL
= 1, IS_NULL
= 2,
72 VALUE_SEPARATOR
= "\ 1",
73 BULK_INSERT_COUNT
= 200,
74 BULK_INSERT_HIGH_COUNT
= 200,
75 QUERY_FORMAT_WILDCARD
= 1,
76 QUERY_FORMAT_NO_QUOTES
= 2,
79 * Serialized string separated by and bookended with VALUE_SEPARATOR
81 SERIALIZE_SEPARATOR_BOOKEND
= 1,
83 * @deprecated format separated by VALUE_SEPARATOR
85 SERIALIZE_SEPARATOR_TRIMMED
= 2,
87 * Recommended serialization format
91 * @deprecated format using php serialize()
95 * Comma separated string, no quotes, no spaces
100 * Define entities that shouldn't be created or deleted when creating/ deleting
101 * test objects - this prevents world regions, countries etc from being added / deleted
104 static $_testEntitiesToSkip = array();
106 * The factory class for this application.
109 static $_factory = NULL;
111 static $_checkedSqlFunctionsExist = FALSE;
114 * https://issues.civicrm.org/jira/browse/CRM-17748
115 * internal variable for DAO to hold per-query settings
117 protected $_options = array();
122 * @return \CRM_Core_DAO
124 public function __construct() {
126 $this->__table
= $this->getTableName();
129 public function __clone() {
130 if (!empty($this->_DB_resultid
)) {
131 $this->resultCopies++
;
138 public function __destruct() {
139 if ($this->resultCopies
=== 0) {
142 $this->resultCopies
--;
146 * Empty definition for virtual function.
148 public static function getTableName() {
153 * Initialize the DAO object.
156 * The database connection string.
158 public static function init($dsn) {
159 Civi
::$statics[__CLASS__
]['init'] = 1;
160 $options = &PEAR
::getStaticProperty('DB_DataObject', 'options');
161 $options['database'] = $dsn;
162 if (defined('CIVICRM_DAO_DEBUG')) {
163 self
::DebugLevel(CIVICRM_DAO_DEBUG
);
165 $factory = new CRM_Contact_DAO_Factory();
166 CRM_Core_DAO
::setFactory($factory);
167 $currentModes = CRM_Utils_SQL
::getSqlModes();
168 if (CRM_Utils_Constant
::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System
::isDevelopment())) {
169 if (CRM_Utils_SQL
::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
170 $currentModes[] = 'ONLY_FULL_GROUP_BY';
172 if (!in_array('STRICT_TRANS_TABLES', $currentModes)) {
173 $currentModes = array_merge(array('STRICT_TRANS_TABLES'), $currentModes);
175 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", array(1 => array(implode(',', $currentModes), 'String')));
177 CRM_Core_DAO
::executeQuery('SET NAMES utf8');
178 CRM_Core_DAO
::executeQuery('SET @uniqueID = %1', array(1 => array(CRM_Utils_Request
::id(), 'String')));
184 public static function getConnection() {
185 global $_DB_DATAOBJECT;
186 $dao = new CRM_Core_DAO();
187 return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5
];
191 * Disables usage of the ONLY_FULL_GROUP_BY Mode if necessary
193 public static function disableFullGroupByMode() {
194 $currentModes = CRM_Utils_SQL
::getSqlModes();
195 if (in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
196 $key = array_search('ONLY_FULL_GROUP_BY', $currentModes);
197 unset($currentModes[$key]);
198 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", array(1 => array(implode(',', $currentModes), 'String')));
203 * Re-enables ONLY_FULL_GROUP_BY sql_mode as necessary..
205 public static function reenableFullGroupByMode() {
206 $currentModes = CRM_Utils_SQL
::getSqlModes();
207 if (!in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
208 $currentModes[] = 'ONLY_FULL_GROUP_BY';
209 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", array(1 => array(implode(',', $currentModes), 'String')));
214 * @param string $fieldName
216 * @param array $params
218 protected function assignTestFK($fieldName, $fieldDef, $params) {
219 $required = CRM_Utils_Array
::value('required', $fieldDef);
220 $FKClassName = CRM_Utils_Array
::value('FKClassName', $fieldDef);
221 $dbName = $fieldDef['name'];
222 $daoName = str_replace('_BAO_', '_DAO_', get_class($this));
224 // skip the FK if it is not required
225 // if it's contact id we should create even if not required
226 // we'll have a go @ fetching first though
227 // we WILL create campaigns though for so tests with a campaign pseudoconstant will complete
228 if ($FKClassName === 'CRM_Campaign_DAO_Campaign' && $daoName != $FKClassName) {
231 if (!$required && $dbName != 'contact_id') {
232 $fkDAO = new $FKClassName();
233 if ($fkDAO->find(TRUE)) {
234 $this->$dbName = $fkDAO->id
;
239 elseif (in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)) {
240 $depObject = new $FKClassName();
241 $depObject->find(TRUE);
242 $this->$dbName = $depObject->id
;
245 elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $fieldName == 'member_of_contact_id') {
246 // FIXME: the fields() metadata is not specific enough
247 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, array('contact_type' => 'Organization'));
248 $this->$dbName = $depObject->id
;
252 //if it is required we need to generate the dependency object first
253 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, CRM_Utils_Array
::value($dbName, $params, 1));
254 $this->$dbName = $depObject->id
;
260 * Generate and assign an arbitrary value to a field of a test object.
262 * @param string $fieldName
263 * @param array $fieldDef
264 * @param int $counter
265 * The globally-unique ID of the test object.
267 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
268 $dbName = $fieldDef['name'];
269 $daoName = get_class($this);
272 if (!$handled && $dbName == 'contact_sub_type') {
273 //coming up with a rule to set this is too complex let's not set it
277 // Pick an option value if needed
278 if (!$handled && $fieldDef['type'] !== CRM_Utils_Type
::T_BOOLEAN
) {
279 $options = $daoName::buildOptions($dbName, 'create');
281 $this->$dbName = key($options);
287 switch ($fieldDef['type']) {
288 case CRM_Utils_Type
::T_INT
:
289 case CRM_Utils_Type
::T_FLOAT
:
290 case CRM_Utils_Type
::T_MONEY
:
291 if (isset($fieldDef['precision'])) {
292 // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']);
293 $this->$dbName = CRM_Utils_Number
::createTruncatedDecimal($counter, $fieldDef['precision']);
296 $this->$dbName = $counter;
300 case CRM_Utils_Type
::T_BOOLEAN
:
301 if (isset($fieldDef['default'])) {
302 $this->$dbName = $fieldDef['default'];
304 elseif ($fieldDef['name'] == 'is_deleted' ||
$fieldDef['name'] == 'is_test') {
312 case CRM_Utils_Type
::T_DATE
:
313 case CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
:
314 $this->$dbName = '19700101';
315 if ($dbName == 'end_date') {
316 // put this in the future
317 $this->$dbName = '20200101';
321 case CRM_Utils_Type
::T_TIMESTAMP
:
322 $this->$dbName = '19700201000000';
325 case CRM_Utils_Type
::T_TIME
:
326 CRM_Core_Error
::fatal("T_TIME shouldn't be used.");
327 //$object->$dbName='000000';
329 case CRM_Utils_Type
::T_CCNUM
:
330 $this->$dbName = '4111 1111 1111 1111';
333 case CRM_Utils_Type
::T_URL
:
334 $this->$dbName = 'http://www.civicrm.org';
337 case CRM_Utils_Type
::T_STRING
:
338 case CRM_Utils_Type
::T_BLOB
:
339 case CRM_Utils_Type
::T_MEDIUMBLOB
:
340 case CRM_Utils_Type
::T_TEXT
:
341 case CRM_Utils_Type
::T_LONGTEXT
:
342 case CRM_Utils_Type
::T_EMAIL
:
344 // WAS: if (isset($value['enumValues'])) {
345 // TODO: see if this works with all pseudoconstants
346 if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) {
347 if (isset($fieldDef['default'])) {
348 $this->$dbName = $fieldDef['default'];
351 $options = CRM_Core_PseudoConstant
::get($daoName, $fieldName);
352 if (is_array($options)) {
353 $this->$dbName = $options[0];
356 $defaultValues = explode(',', $options);
357 $this->$dbName = $defaultValues[0];
362 $this->$dbName = $dbName . '_' . $counter;
363 $maxlength = CRM_Utils_Array
::value('maxlength', $fieldDef);
364 if ($maxlength > 0 && strlen($this->$dbName) > $maxlength) {
365 $this->$dbName = substr($this->$dbName, 0, $fieldDef['maxlength']);
373 * Reset the DAO object.
375 * DAO is kinda crappy in that there is an unwritten rule of one query per DAO.
377 * We attempt to get around this crappy restriction by resetting some of DAO's internal fields. Use this with caution
379 public function reset() {
381 foreach (array_keys($this->table()) as $field) {
382 unset($this->$field);
386 * reset the various DB_DAO structures manually
388 $this->_query
= array();
395 * @param string $tableName
399 public static function getLocaleTableName($tableName) {
402 $tables = CRM_Core_I18n_Schema
::schemaStructureTables();
403 if (in_array($tableName, $tables)) {
404 return $tableName . $dbLocale;
411 * Execute a query by the current DAO, localizing it along the way (if needed).
413 * @param string $query
414 * The SQL query for execution.
415 * @param bool $i18nRewrite
416 * Whether to rewrite the query.
419 * the current DAO object after the query execution
421 public function query($query, $i18nRewrite = TRUE) {
422 // rewrite queries that should use $dbLocale-based views for multi-language installs
423 global $dbLocale, $_DB_DATAOBJECT;
425 if (empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
])) {
426 // Will force connection to be populated per CRM-20541.
430 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
431 $orig_options = $conn->options
;
432 $this->_setDBOptions($this->_options
);
434 if ($i18nRewrite and $dbLocale) {
435 $query = CRM_Core_I18n_Schema
::rewriteQuery($query);
438 $ret = parent
::query($query);
440 $this->_setDBOptions($orig_options);
445 * Static function to set the factory instance for this class.
447 * @param object $factory
448 * The factory application object.
450 public static function setFactory(&$factory) {
451 self
::$_factory = &$factory;
455 * Factory method to instantiate a new object from a table name.
457 * @param string $table
458 * @return \DataObject|\PEAR_Error
460 public function factory($table = '') {
461 if (!isset(self
::$_factory)) {
462 return parent
::factory($table);
465 return self
::$_factory->create($table);
469 * Initialization for all DAO objects. Since we access DB_DO programatically
470 * we need to set the links manually.
472 public function initialize() {
474 if (empty(Civi
::$statics[__CLASS__
]['init'])) {
475 // CRM_Core_DAO::init() must be called before CRM_Core_DAO->initialize().
476 // This occurs very early in bootstrap - error handlers may not be wired up.
477 echo "Inconsistent system initialization sequence. Premature access of (" . get_class($this) . ")";
478 CRM_Utils_System
::civiExit();
483 * Defines the default key as 'id'.
487 public function keys() {
496 * Tells DB_DataObject which keys use autoincrement.
497 * 'id' is autoincrementing by default.
502 public function sequenceKey() {
503 static $sequenceKeys;
504 if (!isset($sequenceKeys)) {
505 $sequenceKeys = array('id', TRUE);
507 return $sequenceKeys;
511 * Returns list of FK relationships.
515 * Array of CRM_Core_Reference_Interface
517 public static function getReferenceColumns() {
522 * Returns all the column names of this table.
527 public static function &fields() {
533 * Get/set an associative array of table columns
538 public function table() {
539 $fields = $this->fields();
543 foreach ($fields as $name => $value) {
544 $table[$value['name']] = $value['type'];
545 if (!empty($value['required'])) {
546 $table[$value['name']] +
= self
::DB_DAO_NOTNULL
;
559 * @return CRM_Core_DAO
561 public function save($hook = TRUE) {
562 if (!empty($this->id
)) {
566 $event = new \Civi\Core\DAO\Event\
PostUpdate($this);
567 \Civi
::service('dispatcher')->dispatch("civi.dao.postUpdate", $event);
574 $event = new \Civi\Core\DAO\Event\
PostUpdate($this);
575 \Civi
::service('dispatcher')->dispatch("civi.dao.postInsert", $event);
581 CRM_Utils_Hook
::postSave($this);
588 * Deletes items from table which match current objects variables.
590 * Returns the true on success
594 * Designed to be extended
596 * $object = new mytable();
598 * echo $object->delete(); // builds a conditon
600 * $object = new mytable();
601 * $object->whereAdd('age > 12');
603 * $object->orderBy('age DESC');
604 * $object->delete(true); // dont use object vars, use the conditions, limit and order.
606 * @param bool $useWhere (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then
607 * we will build the condition only using the whereAdd's. Default is to
608 * build the condition only using the object parameters.
610 * * @return mixed Int (No. of rows affected) on success, false on failure, 0 on no data affected
612 public function delete($useWhere = FALSE) {
613 $preEvent = new \Civi\Core\DAO\Event\
PreDelete($this);
614 \Civi
::service('dispatcher')->dispatch("civi.dao.preDelete", $preEvent);
616 $result = parent
::delete($useWhere);
618 $event = new \Civi\Core\DAO\Event\
PostDelete($this, $result);
619 \Civi
::service('dispatcher')->dispatch("civi.dao.postDelete", $event);
626 * @param bool $created
628 public function log($created = FALSE) {
631 if (!$this->getLog()) {
636 $session = CRM_Core_Session
::singleton();
637 $cid = $session->get('userID');
640 // return is we dont have handle to FK
645 $dao = new CRM_Core_DAO_Log();
646 $dao->entity_table
= $this->getTableName();
647 $dao->entity_id
= $this->id
;
648 $dao->modified_id
= $cid;
649 $dao->modified_date
= date("YmdHis");
654 * Given an associative array of name/value pairs, extract all the values
655 * that belong to this object and initialize the object with said values
657 * @param array $params
658 * (reference ) associative array of name/value pairs.
659 * @param bool $serializeArrays
660 * Should arrays that are passed in be serialised according to the metadata.
661 * Eventually this should be always true / gone, but in the interests of caution
662 * it is being grandfathered in. In general an array is not valid on the DAO
663 * but there may be instances where this function is called & then some handling
664 * takes place on the would-be array.
667 * Did we copy all null values into the object
669 public function copyValues(&$params, $serializeArrays = FALSE) {
670 $fields = $this->fields();
672 foreach ($fields as $name => $value) {
673 $dbName = $value['name'];
674 if (array_key_exists($dbName, $params)) {
675 $pValue = $params[$dbName];
678 elseif (array_key_exists($name, $params)) {
679 $pValue = $params[$name];
686 // if there is no value then make the variable NULL
688 if ($pValue === '') {
689 $this->$dbName = 'null';
691 elseif ($serializeArrays && is_array($pValue) && !empty($value['serialize'])) {
692 $this->$dbName = CRM_Core_DAO
::serializeField($pValue, $value['serialize']);
696 if (!$serializeArrays && is_array($pValue) && !empty($value['serialize'])) {
697 Civi
::log()->warning(ts('use copyParams to serialize arrays (' . __CLASS__
. '.' . $name . ')'), ['civi.tag' => 'deprecated']);
699 $maxLength = CRM_Utils_Array
::value('maxlength', $value);
700 if (!is_array($pValue) && $maxLength && mb_strlen($pValue) > $maxLength
701 && empty($value['pseudoconstant'])
703 Civi
::log()->warning(ts('A string for field $dbName has been truncated. The original string was %1', [CRM_Utils_Type
::escape($pValue, 'String')]));
704 // The string is too long - what to do what to do? Well losing data is generally bad so lets' truncate
705 $pValue = CRM_Utils_String
::ellipsify($pValue, $maxLength);
707 $this->$dbName = $pValue;
716 * Store all the values from this object in an associative array
717 * this is a destructive store, calling function is responsible
718 * for keeping sanity of id's.
720 * @param object $object
721 * The object that we are extracting data from.
722 * @param array $values
723 * (reference ) associative array of name/value pairs.
725 public static function storeValues(&$object, &$values) {
726 $fields = $object->fields();
727 foreach ($fields as $name => $value) {
728 $dbName = $value['name'];
729 if (isset($object->$dbName) && $object->$dbName !== 'null') {
730 $values[$dbName] = $object->$dbName;
731 if ($name != $dbName) {
732 $values[$name] = $object->$dbName;
739 * Create an attribute for this specific field. We only do this for strings and text
741 * @param array $field
742 * The field under task.
745 * the attributes for the object
747 public static function makeAttribute($field) {
749 if (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_STRING
) {
750 $maxLength = CRM_Utils_Array
::value('maxlength', $field);
751 $size = CRM_Utils_Array
::value('size', $field);
752 if ($maxLength ||
$size) {
753 $attributes = array();
755 $attributes['maxlength'] = $maxLength;
758 $attributes['size'] = $size;
763 elseif (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_TEXT
) {
764 $rows = CRM_Utils_Array
::value('rows', $field);
768 $cols = CRM_Utils_Array
::value('cols', $field);
773 $attributes = array();
774 $attributes['rows'] = $rows;
775 $attributes['cols'] = $cols;
778 elseif (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_INT || CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_FLOAT || CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_MONEY
) {
779 $attributes['size'] = 6;
780 $attributes['maxlength'] = 14;
788 * Get the size and maxLength attributes for this text field.
789 * (or for all text fields) in the DAO object.
791 * @param string $class
793 * @param string $fieldName
794 * Field that i'm interested in or null if.
795 * you want the attributes for all DAO text fields
798 * assoc array of name => attribute pairs
800 public static function getAttribute($class, $fieldName = NULL) {
801 $object = new $class();
802 $fields = $object->fields();
803 if ($fieldName != NULL) {
804 $field = CRM_Utils_Array
::value($fieldName, $fields);
805 return self
::makeAttribute($field);
808 $attributes = array();
809 foreach ($fields as $name => $field) {
810 $attribute = self
::makeAttribute($field);
812 $attributes[$name] = $attribute;
816 if (!empty($attributes)) {
824 * Check if there is a record with the same name in the db.
826 * @param string $value
827 * The value of the field we are checking.
828 * @param string $daoName
829 * The dao object name.
830 * @param string $daoID
831 * The id of the object being updated. u can change your name.
832 * as long as there is no conflict
833 * @param string $fieldName
834 * The name of the field in the DAO.
836 * @param string $domainID
837 * The id of the domain. Object exists only for the given domain.
840 * true if object exists
842 public static function objectExists($value, $daoName, $daoID, $fieldName = 'name', $domainID = NULL) {
843 $object = new $daoName();
844 $object->$fieldName = $value;
846 $object->domain_id
= $domainID;
849 if ($object->find(TRUE)) {
850 return ($daoID && $object->id
== $daoID) ?
TRUE : FALSE;
858 * Check if there is a given column in a specific table.
861 * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
863 * @param string $tableName
864 * @param string $columnName
865 * @param bool $i18nRewrite
866 * Whether to rewrite the query on multilingual setups.
869 * true if exists, else false
871 public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
872 return CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
876 * Scans all the tables using a slow query and table name.
880 public static function getTableNames() {
881 $dao = CRM_Core_DAO
::executeQuery(
883 FROM information_schema.TABLES
884 WHERE TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
885 AND TABLE_NAME LIKE 'civicrm_%'
886 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
887 AND TABLE_NAME NOT LIKE '%_temp%'
890 while ($dao->fetch()) {
891 $values[] = $dao->TABLE_NAME
;
897 * @param int $maxTablesToCheck
901 public static function isDBMyISAM($maxTablesToCheck = 10) {
902 return CRM_Core_DAO
::singleValueQuery(
904 FROM information_schema.TABLES
905 WHERE ENGINE = 'MyISAM'
906 AND TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
907 AND TABLE_NAME LIKE 'civicrm_%'
908 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
909 AND TABLE_NAME NOT LIKE '%_temp%'
910 AND TABLE_NAME NOT LIKE 'civicrm_tmp_%'
915 * Get the name of the CiviCRM database.
919 public static function getDatabaseName() {
920 $daoObj = new CRM_Core_DAO();
921 return $daoObj->database();
925 * Checks if a constraint exists for a specified table.
927 * @param string $tableName
928 * @param string $constraint
931 * true if constraint exists, false otherwise
933 public static function checkConstraintExists($tableName, $constraint) {
934 static $show = array();
936 if (!array_key_exists($tableName, $show)) {
937 $query = "SHOW CREATE TABLE $tableName";
938 $dao = CRM_Core_DAO
::executeQuery($query);
940 if (!$dao->fetch()) {
941 CRM_Core_Error
::fatal();
944 $show[$tableName] = $dao->Create_Table
;
947 return preg_match("/\b$constraint\b/i", $show[$tableName]) ?
TRUE : FALSE;
951 * Checks if CONSTRAINT keyword exists for a specified table.
953 * @param array $tables
958 * true if CONSTRAINT keyword exists, false otherwise
960 public static function schemaRequiresRebuilding($tables = array("civicrm_contact")) {
962 foreach ($tables as $tableName) {
963 if (!array_key_exists($tableName, $show)) {
964 $query = "SHOW CREATE TABLE $tableName";
965 $dao = CRM_Core_DAO
::executeQuery($query);
967 if (!$dao->fetch()) {
968 CRM_Core_Error
::fatal();
971 $show[$tableName] = $dao->Create_Table
;
974 $result = preg_match("/\bCONSTRAINT\b\s/i", $show[$tableName]) ?
TRUE : FALSE;
975 if ($result == TRUE) {
986 * Checks if the FK constraint name is in the format 'FK_tableName_columnName'
987 * for a specified column of a table.
989 * @param string $tableName
990 * @param string $columnName
993 * true if in format, false otherwise
995 public static function checkFKConstraintInFormat($tableName, $columnName) {
996 static $show = array();
998 if (!array_key_exists($tableName, $show)) {
999 $query = "SHOW CREATE TABLE $tableName";
1000 $dao = CRM_Core_DAO
::executeQuery($query);
1002 if (!$dao->fetch()) {
1003 CRM_Core_Error
::fatal();
1006 $show[$tableName] = $dao->Create_Table
;
1008 $constraint = "`FK_{$tableName}_{$columnName}`";
1009 $pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
1010 return preg_match(sprintf($pattern, $constraint), $show[$tableName]) ?
TRUE : FALSE;
1014 * Check whether a specific column in a specific table has always the same value.
1016 * @param string $tableName
1017 * @param string $columnName
1018 * @param string $columnValue
1021 * true if the value is always $columnValue, false otherwise
1023 public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
1024 $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
1025 $dao = CRM_Core_DAO
::executeQuery($query);
1026 $result = $dao->fetch() ?
FALSE : TRUE;
1031 * Check whether a specific column in a specific table is always NULL.
1033 * @param string $tableName
1034 * @param string $columnName
1037 * true if if the value is always NULL, false otherwise
1039 public static function checkFieldIsAlwaysNull($tableName, $columnName) {
1040 $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
1041 $dao = CRM_Core_DAO
::executeQuery($query);
1042 $result = $dao->fetch() ?
FALSE : TRUE;
1047 * Check if there is a given table in the database.
1049 * @param string $tableName
1052 * true if exists, else false
1054 public static function checkTableExists($tableName) {
1059 $params = array(1 => array($tableName, 'String'));
1061 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1062 $result = $dao->fetch() ?
TRUE : FALSE;
1071 public function checkVersion($version) {
1076 $dbVersion = CRM_Core_DAO
::singleValueQuery($query);
1077 return trim($version) == trim($dbVersion) ?
TRUE : FALSE;
1081 * Find a DAO object for the given ID and return it.
1084 * Id of the DAO object being searched for.
1086 * @return CRM_Core_DAO
1087 * Object of the type of the class that called this function.
1091 public static function findById($id) {
1092 $object = new static();
1094 if (!$object->find(TRUE)) {
1095 throw new Exception("Unable to find a " . get_called_class() . " with id {$id}.");
1101 * Returns all results as array-encoded records.
1105 public function fetchAll() {
1107 while ($this->fetch()) {
1108 $result[] = $this->toArray();
1114 * Return the results as PHP generator.
1116 * @param string $type
1117 * Whether the generator yields 'dao' objects or 'array's.
1119 public function fetchGenerator($type = 'dao') {
1120 while ($this->fetch()) {
1127 yield
$this->toArray();
1131 throw new \
RuntimeException("Invalid record type ($type)");
1137 * Returns a singular value.
1139 * @return mixed|NULL
1141 public function fetchValue() {
1142 $result = $this->getDatabaseResult();
1143 $row = $result->fetchRow();
1153 * Get all the result records as mapping between columns.
1155 * @param string $keyColumn
1157 * @param string $valueColumn
1160 * Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
1162 public function fetchMap($keyColumn, $valueColumn) {
1164 while ($this->fetch()) {
1165 $result[$this->{$keyColumn}] = $this->{$valueColumn};
1171 * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
1173 * @param string $daoName
1174 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1175 * @param int $searchValue
1176 * Value of the column you want to search by.
1177 * @param string $returnColumn
1178 * Name of the column you want to GET the value of.
1179 * @param string $searchColumn
1180 * Name of the column you want to search by.
1181 * @param bool $force
1182 * Skip use of the cache.
1184 * @return string|null
1185 * Value of $returnColumn in the retrieved record
1187 public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
1189 empty($searchValue) ||
1190 trim(strtolower($searchValue)) == 'null'
1192 // adding this here since developers forget to check for an id
1193 // or for the 'null' (which is a bad DAO kludge)
1194 // and hence we get the first value in the db
1195 CRM_Core_Error
::fatal();
1198 $cacheKey = "{$daoName}:{$searchValue}:{$returnColumn}:{$searchColumn}";
1199 if (self
::$_dbColumnValueCache === NULL) {
1200 self
::$_dbColumnValueCache = array();
1203 if (!array_key_exists($cacheKey, self
::$_dbColumnValueCache) ||
$force) {
1204 $object = new $daoName();
1205 $object->$searchColumn = $searchValue;
1206 $object->selectAdd();
1207 $object->selectAdd($returnColumn);
1210 if ($object->find(TRUE)) {
1211 $result = $object->$returnColumn;
1215 self
::$_dbColumnValueCache[$cacheKey] = $result;
1217 return self
::$_dbColumnValueCache[$cacheKey];
1221 * Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
1223 * @param string $daoName
1224 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1225 * @param int $searchValue
1226 * Value of the column you want to search by.
1227 * @param string $setColumn
1228 * Name of the column you want to SET the value of.
1229 * @param string $setValue
1230 * SET the setColumn to this value.
1231 * @param string $searchColumn
1232 * Name of the column you want to search by.
1235 * true if we found and updated the object, else false
1237 public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
1238 $object = new $daoName();
1239 $object->selectAdd();
1240 $object->selectAdd("$searchColumn, $setColumn");
1241 $object->$searchColumn = $searchValue;
1243 if ($object->find(TRUE)) {
1244 $object->$setColumn = $setValue;
1245 if ($object->save()) {
1256 * @param array|object $sort either array or CRM_Utils_Sort
1257 * @param string $default
1258 * Default sort value.
1262 public static function getSortString($sort, $default = NULL) {
1263 // check if sort is of type CRM_Utils_Sort
1264 if (is_a($sort, 'CRM_Utils_Sort')) {
1265 return $sort->orderBy();
1270 // is it an array specified as $field => $sortDirection ?
1272 foreach ($sort as $k => $v) {
1273 $sortString .= "$k $v,";
1275 return rtrim($sortString, ',');
1281 * Fetch object based on array of properties.
1283 * @param string $daoName
1284 * Name of the dao object.
1285 * @param array $params
1286 * (reference ) an assoc array of name/value pairs.
1287 * @param array $defaults
1288 * (reference ) an assoc array to hold the flattened values.
1289 * @param array $returnProperities
1290 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1293 * an object of type referenced by daoName
1295 public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
1296 $object = new $daoName();
1297 $object->copyValues($params);
1299 // return only specific fields if returnproperties are sent
1300 if (!empty($returnProperities)) {
1301 $object->selectAdd();
1302 $object->selectAdd(implode(',', $returnProperities));
1305 if ($object->find(TRUE)) {
1306 self
::storeValues($object, $defaults);
1313 * Delete the object records that are associated with this contact.
1315 * @param string $daoName
1316 * Name of the dao object.
1317 * @param int $contactId
1318 * Id of the contact to delete.
1320 public static function deleteEntityContact($daoName, $contactId) {
1321 $object = new $daoName();
1323 $object->entity_table
= 'civicrm_contact';
1324 $object->entity_id
= $contactId;
1329 * Execute an unbuffered query.
1331 * This is a wrapper around new functionality exposed with CRM-17748.
1333 * @param string $query query to be executed
1335 * @param array $params
1336 * @param bool $abort
1337 * @param null $daoName
1338 * @param bool $freeDAO
1339 * @param bool $i18nRewrite
1340 * @param bool $trapException
1342 * @return CRM_Core_DAO
1343 * Object that points to an unbuffered result set
1345 static public function executeUnbufferedQuery(
1351 $i18nRewrite = TRUE,
1352 $trapException = FALSE
1355 return self
::executeQuery(
1363 array('result_buffering' => 0)
1370 * @param string $query
1371 * Query to be executed.
1373 * @param array $params
1374 * @param bool $abort
1375 * @param null $daoName
1376 * @param bool $freeDAO
1377 * @param bool $i18nRewrite
1378 * @param bool $trapException
1379 * @param array $options
1381 * @return CRM_Core_DAO|object
1382 * object that holds the results of the query
1383 * NB - if this is defined as just returning a DAO phpstorm keeps pointing
1384 * out all the properties that are not part of the DAO
1386 public static function &executeQuery(
1392 $i18nRewrite = TRUE,
1393 $trapException = FALSE,
1396 $queryStr = self
::composeQuery($query, $params, $abort);
1399 $dao = new CRM_Core_DAO();
1402 $dao = new $daoName();
1405 if ($trapException) {
1406 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
1409 if ($dao->isValidOption($options)) {
1410 $dao->setOptions($options);
1413 $result = $dao->query($queryStr, $i18nRewrite);
1415 // since it is unbuffered, ($dao->N==0) is true. This blocks the standard fetch() mechanism.
1416 if (CRM_Utils_Array
::value('result_buffering', $options) === 0) {
1420 if (is_a($result, 'DB_Error')) {
1425 preg_match('/^(insert|update|delete|create|drop|replace)/i', $queryStr)
1427 // we typically do this for insert/update/delete statements OR if explicitly asked to
1434 * Wrapper to validate internal DAO options before passing to DB_mysql/DB_Common level
1436 * @param array $options
1439 * Provided options are valid
1441 public function isValidOption($options) {
1443 $validOptions = array(
1450 if (empty($options)) {
1454 foreach (array_keys($options) as $option) {
1455 if (!in_array($option, $validOptions)) {
1465 * Execute a query and get the single result.
1467 * @param string $query
1468 * Query to be executed.
1469 * @param array $params
1470 * @param bool $abort
1471 * @param bool $i18nRewrite
1472 * @return string|null
1473 * the result of the query if any
1476 public static function &singleValueQuery(
1482 $queryStr = self
::composeQuery($query, $params, $abort);
1484 static $_dao = NULL;
1487 $_dao = new CRM_Core_DAO();
1490 $_dao->query($queryStr, $i18nRewrite);
1492 $result = $_dao->getDatabaseResult();
1495 $row = $result->fetchRow();
1505 * Compose the query by merging the parameters into it.
1507 * @param string $query
1508 * @param array $params
1509 * @param bool $abort
1514 public static function composeQuery($query, $params, $abort = TRUE) {
1516 foreach ($params as $key => $item) {
1517 if (is_numeric($key)) {
1518 if (CRM_Utils_Type
::validate($item[0], $item[1]) !== NULL) {
1519 $item[0] = self
::escapeString($item[0]);
1520 if ($item[1] == 'String' ||
1521 $item[1] == 'Memo' ||
1524 // Support class constants stipulating wildcard characters and/or
1525 // non-quoting of strings. Also support legacy code which may be
1526 // passing in TRUE or 1 for $item[2], which used to indicate the
1527 // use of wildcard characters.
1528 if (!empty($item[2])) {
1529 if ($item[2] & CRM_Core_DAO
::QUERY_FORMAT_WILDCARD ||
$item[2] === TRUE) {
1530 $item[0] = "'%{$item[0]}%'";
1532 elseif (!($item[2] & CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
)) {
1533 $item[0] = "'{$item[0]}'";
1537 $item[0] = "'{$item[0]}'";
1541 if (($item[1] == 'Date' ||
$item[1] == 'Timestamp') &&
1542 strlen($item[0]) == 0
1547 $tr['%' . $key] = $item[0];
1550 CRM_Core_Error
::fatal("{$item[0]} is not of type {$item[1]}");
1555 return strtr($query, $tr);
1561 public static function freeResult($ids = NULL) {
1562 global $_DB_DATAOBJECT;
1565 if (!$_DB_DATAOBJECT ||
1566 !isset($_DB_DATAOBJECT['RESULTS'])
1570 $ids = array_keys($_DB_DATAOBJECT['RESULTS']);
1573 foreach ($ids as $id) {
1574 if (isset($_DB_DATAOBJECT['RESULTS'][$id])) {
1575 $_DB_DATAOBJECT['RESULTS'][$id]->free();
1576 unset($_DB_DATAOBJECT['RESULTS'][$id]);
1579 if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$id])) {
1580 unset($_DB_DATAOBJECT['RESULTFIELDS'][$id]);
1586 * Make a shallow copy of an object and all the fields in the object.
1588 * @param string $daoName
1590 * @param array $criteria
1591 * Array of all the fields & values.
1592 * on which basis to copy
1593 * @param array $newData
1594 * Array of all the fields & values.
1595 * to be copied besides the other fields
1596 * @param string $fieldsFix
1597 * Array of fields that you want to prefix/suffix/replace.
1598 * @param string $blockCopyOfDependencies
1599 * Fields that you want to block from.
1602 * @return CRM_Core_DAO
1603 * the newly created copy of the object
1605 public static function ©Generic($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
1606 $object = new $daoName();
1608 $object->id
= $criteria['id'];
1611 foreach ($criteria as $key => $value) {
1612 $object->$key = $value;
1617 while ($object->fetch()) {
1619 // all the objects except with $blockCopyOfDependencies set
1620 // be copied - addresses #CRM-1962
1622 if ($blockCopyOfDependencies && $object->$blockCopyOfDependencies) {
1626 $newObject = new $daoName();
1628 $fields = $object->fields();
1629 if (!is_array($fieldsFix)) {
1630 $fieldsToPrefix = array();
1631 $fieldsToSuffix = array();
1632 $fieldsToReplace = array();
1634 if (!empty($fieldsFix['prefix'])) {
1635 $fieldsToPrefix = $fieldsFix['prefix'];
1637 if (!empty($fieldsFix['suffix'])) {
1638 $fieldsToSuffix = $fieldsFix['suffix'];
1640 if (!empty($fieldsFix['replace'])) {
1641 $fieldsToReplace = $fieldsFix['replace'];
1644 foreach ($fields as $name => $value) {
1645 if ($name == 'id' ||
$value['name'] == 'id') {
1646 // copy everything but the id!
1650 $dbName = $value['name'];
1651 $type = CRM_Utils_Type
::typeToString($value['type']);
1652 $newObject->$dbName = $object->$dbName;
1653 if (isset($fieldsToPrefix[$dbName])) {
1654 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1656 if (isset($fieldsToSuffix[$dbName])) {
1657 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1659 if (isset($fieldsToReplace[$dbName])) {
1660 $newObject->$dbName = $fieldsToReplace[$dbName];
1663 if ($type == 'Timestamp' ||
$type == 'Date') {
1664 $newObject->$dbName = CRM_Utils_Date
::isoToMysql($newObject->$dbName);
1668 $newObject->copyValues($newData);
1672 CRM_Utils_Hook
::post('create', CRM_Core_DAO_AllCoreTables
::getBriefName($daoName), $newObject->id
, $newObject);
1679 * Cascade update through related entities.
1681 * @param string $daoName
1684 * @param array $newData
1686 * @return CRM_Core_DAO|null
1688 public static function cascadeUpdate($daoName, $fromId, $toId, $newData = array()) {
1689 $object = new $daoName();
1690 $object->id
= $fromId;
1692 if ($object->find(TRUE)) {
1693 $newObject = new $daoName();
1694 $newObject->id
= $toId;
1696 if ($newObject->find(TRUE)) {
1697 $fields = $object->fields();
1698 foreach ($fields as $name => $value) {
1699 if ($name == 'id' ||
$value['name'] == 'id') {
1700 // copy everything but the id!
1704 $colName = $value['name'];
1705 $newObject->$colName = $object->$colName;
1707 if (substr($name, -5) == '_date' ||
1708 substr($name, -10) == '_date_time'
1710 $newObject->$colName = CRM_Utils_Date
::isoToMysql($newObject->$colName);
1713 foreach ($newData as $k => $v) {
1714 $newObject->$k = $v;
1724 * Given the component id, compute the contact id
1725 * since its used for things like send email
1727 * @param $componentIDs
1728 * @param string $tableName
1729 * @param string $idField
1733 public static function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
1734 $contactIDs = array();
1736 if (empty($componentIDs)) {
1740 $IDs = implode(',', $componentIDs);
1744 WHERE $idField IN ( $IDs )
1747 $dao = CRM_Core_DAO
::executeQuery($query);
1748 while ($dao->fetch()) {
1749 $contactIDs[] = $dao->contact_id
;
1755 * Fetch object based on array of properties.
1757 * @param string $daoName
1758 * Name of the dao object.
1759 * @param string $fieldIdName
1760 * @param int $fieldId
1762 * @param array $returnProperities
1763 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1766 * an object of type referenced by daoName
1768 public static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
1769 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php";
1770 $object = new $daoName();
1771 $object->$fieldIdName = $fieldId;
1773 // return only specific fields if returnproperties are sent
1774 if (!empty($returnProperities)) {
1775 $object->selectAdd();
1776 $object->selectAdd('id');
1777 $object->selectAdd(implode(',', $returnProperities));
1781 while ($object->fetch()) {
1782 $defaults = array();
1783 self
::storeValues($object, $defaults);
1784 $details[$object->id
] = $defaults;
1791 * Drop all CiviCRM tables.
1793 * @throws \CRM_Exception
1795 public static function dropAllTables() {
1797 // first drop all the custom tables we've created
1798 CRM_Core_BAO_CustomGroup
::dropAllTables();
1800 // drop all multilingual views
1801 CRM_Core_I18n_Schema
::dropAllViews();
1803 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
,
1804 dirname(__FILE__
) . DIRECTORY_SEPARATOR
.
1805 '..' . DIRECTORY_SEPARATOR
.
1806 '..' . DIRECTORY_SEPARATOR
.
1807 'sql' . DIRECTORY_SEPARATOR
.
1808 'civicrm_drop.mysql'
1817 public static function escapeString($string) {
1818 static $_dao = NULL;
1820 // If this is an atypical case (e.g. preparing .sql file before CiviCRM
1821 // has been installed), then we fallback DB-less str_replace escaping, as
1822 // we can't use mysqli_real_escape_string, as there is no DB connection.
1823 // Note: In typical usage, escapeString() will only check one conditional
1824 // ("if !$_dao") rather than two conditionals ("if !defined(DSN)")
1825 if (!defined('CIVICRM_DSN')) {
1826 // See http://php.net/manual/en/mysqli.real-escape-string.php for the
1827 // list of characters mysqli_real_escape_string escapes.
1828 $search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
1829 $replace = array("\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z");
1830 return str_replace($search, $replace, $string);
1832 $_dao = new CRM_Core_DAO();
1834 return $_dao->escape($string);
1838 * Escape a list of strings for use with "WHERE X IN (...)" queries.
1840 * @param array $strings
1841 * @param string $default
1842 * the value to use if $strings has no elements.
1844 * eg "abc","def","ghi"
1846 public static function escapeStrings($strings, $default = NULL) {
1847 static $_dao = NULL;
1849 $_dao = new CRM_Core_DAO();
1852 if (empty($strings)) {
1856 $escapes = array_map(array($_dao, 'escape'), $strings);
1857 return '"' . implode('","', $escapes) . '"';
1865 public static function escapeWildCardString($string) {
1867 // ensure we escape the single characters % and _ which are mysql wild
1868 // card characters and could come in via sortByCharacter
1869 // note that mysql does not escape these characters
1870 if ($string && in_array($string,
1871 array('%', '_', '%%', '_%')
1874 return '\\' . $string;
1877 return self
::escapeString($string);
1881 * Creates a test object, including any required objects it needs via recursion
1882 * createOnly: only create in database, do not store or return the objects (useful for perf testing)
1883 * ONLY USE FOR TESTING
1885 * @param string $daoName
1886 * @param array $params
1887 * @param int $numObjects
1888 * @param bool $createOnly
1890 * @return object|array|NULL
1891 * NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
1893 public static function createTestObject(
1899 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
1900 // so we re-set here in case
1901 $config = CRM_Core_Config
::singleton();
1902 $config->backtrace
= TRUE;
1904 static $counter = 0;
1905 CRM_Core_DAO
::$_testEntitiesToSkip = array(
1906 'CRM_Core_DAO_Worldregion',
1907 'CRM_Core_DAO_StateProvince',
1908 'CRM_Core_DAO_Country',
1909 'CRM_Core_DAO_Domain',
1910 'CRM_Financial_DAO_FinancialType',
1911 //because valid ones exist & we use pick them due to pseudoconstant can't reliably create & delete these
1914 // Prefer to instantiate BAO's instead of DAO's (when possible)
1915 // so that assignTestValue()/assignTestFK() can be overloaded.
1916 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
1917 if (class_exists($baoName)) {
1918 $daoName = $baoName;
1921 for ($i = 0; $i < $numObjects; ++
$i) {
1924 /** @var CRM_Core_DAO $object */
1925 $object = new $daoName();
1927 $fields = $object->fields();
1928 foreach ($fields as $fieldName => $fieldDef) {
1929 $dbName = $fieldDef['name'];
1930 $FKClassName = CRM_Utils_Array
::value('FKClassName', $fieldDef);
1931 $required = CRM_Utils_Array
::value('required', $fieldDef);
1933 if (CRM_Utils_Array
::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
1934 $object->$dbName = $params[$dbName];
1937 elseif ($dbName != 'id') {
1938 if ($FKClassName != NULL) {
1939 $object->assignTestFK($fieldName, $fieldDef, $params);
1943 $object->assignTestValue($fieldName, $fieldDef, $counter);
1951 $objects[$i] = $object;
1961 elseif ($numObjects == 1) {
1970 * Deletes the this object plus any dependent objects that are associated with it.
1971 * ONLY USE FOR TESTING
1973 * @param string $daoName
1974 * @param array $params
1976 public static function deleteTestObjects($daoName, $params = array()) {
1977 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
1978 // so we re-set here in case
1979 $config = CRM_Core_Config
::singleton();
1980 $config->backtrace
= TRUE;
1982 $object = new $daoName();
1983 $object->id
= CRM_Utils_Array
::value('id', $params);
1985 $deletions = array(); // array(array(0 => $daoName, 1 => $daoParams))
1986 if ($object->find(TRUE)) {
1988 $fields = $object->fields();
1989 foreach ($fields as $name => $value) {
1991 $dbName = $value['name'];
1993 $FKClassName = CRM_Utils_Array
::value('FKClassName', $value);
1994 $required = CRM_Utils_Array
::value('required', $value);
1995 if ($FKClassName != NULL
1997 && !in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)
1998 && ($required ||
$dbName == 'contact_id')
1999 //I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
2000 // to make this test process pass - line below makes pass for now
2001 && $dbName != 'member_of_contact_id'
2003 $deletions[] = array($FKClassName, array('id' => $object->$dbName)); // x
2010 foreach ($deletions as $deletion) {
2011 CRM_Core_DAO
::deleteTestObjects($deletion[0], $deletion[1]);
2016 * Set defaults when creating new entity.
2017 * (don't call this set defaults as already in use with different signature in some places)
2019 * @param array $params
2022 public static function setCreateDefaults(&$params, $defaults) {
2023 if (!empty($params['id'])) {
2026 foreach ($defaults as $key => $value) {
2027 if (!array_key_exists($key, $params) ||
$params[$key] === NULL) {
2028 $params[$key] = $value;
2034 * @param string $prefix
2035 * @param bool $addRandomString
2036 * @param null $string
2040 * @see CRM_Utils_SQL_TempTable
2042 public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
2043 $tableName = $prefix . "_temp";
2045 if ($addRandomString) {
2047 $tableName .= "_" . $string;
2050 $tableName .= "_" . md5(uniqid('', TRUE));
2058 * @param bool $trigger
2062 public static function checkTriggerViewPermission($view = TRUE, $trigger = TRUE) {
2063 if (\Civi
::settings()->get('logging_no_trigger_permission')) {
2066 // test for create view and trigger permissions and if allowed, add the option to go multilingual
2068 // I'm not sure why we use the getStaticProperty for an error, rather than checking for DB_Error
2069 CRM_Core_TemporaryErrorScope
::ignoreException();
2070 $dao = new CRM_Core_DAO();
2072 $result = $dao->query('CREATE OR REPLACE VIEW civicrm_domain_view AS SELECT * FROM civicrm_domain');
2073 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError') ||
is_a($result, 'DB_Error')) {
2079 $result = $dao->query('CREATE TRIGGER civicrm_domain_trigger BEFORE INSERT ON civicrm_domain FOR EACH ROW BEGIN END');
2080 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError') ||
is_a($result, 'DB_Error')) {
2082 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2087 $dao->query('DROP TRIGGER IF EXISTS civicrm_domain_trigger');
2088 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError')) {
2090 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2097 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2098 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError')) {
2107 * @param null $message
2108 * @param bool $printDAO
2110 public static function debugPrint($message = NULL, $printDAO = TRUE) {
2111 CRM_Utils_System
::xMemory("{$message}: ");
2114 global $_DB_DATAOBJECT;
2116 foreach (array_keys($_DB_DATAOBJECT['RESULTS']) as $id) {
2117 $q[] = $_DB_DATAOBJECT['RESULTS'][$id]->query
;
2119 CRM_Core_Error
::debug('_DB_DATAOBJECT', $q);
2124 * Build a list of triggers via hook and add them to (err, reconcile them
2125 * with) the database.
2127 * @param string $tableName
2128 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2129 * @param bool $force
2134 public static function triggerRebuild($tableName = NULL, $force = FALSE) {
2135 Civi
::service('sql_triggers')->rebuild($tableName, $force);
2139 * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
2140 * @see http://issues.civicrm.org/jira/browse/CRM-13822
2141 * TODO: Alternative solutions might be
2142 * * Stop using functions and find another way to strip numeric characters from phones
2143 * * Give better error messages (currently a missing fn fatals with "unknown error")
2145 public static function checkSqlFunctionsExist() {
2146 if (!self
::$_checkedSqlFunctionsExist) {
2147 self
::$_checkedSqlFunctionsExist = TRUE;
2148 $dao = CRM_Core_DAO
::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
2149 if (!$dao->fetch()) {
2150 self
::triggerRebuild();
2156 * Wrapper function to drop triggers.
2158 * @param string $tableName
2159 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2162 public static function dropTriggers($tableName = NULL) {
2163 Civi
::service('sql_triggers')->dropTriggers($tableName);
2167 * @param array $info
2168 * per hook_civicrm_triggerInfo.
2169 * @param string $onlyTableName
2170 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2173 public static function createTriggers(&$info, $onlyTableName = NULL) {
2174 Civi
::service('sql_triggers')->createTriggers($info, $onlyTableName);
2178 * Given a list of fields, create a list of references.
2180 * @param string $className
2181 * BAO/DAO class name.
2182 * @return array<CRM_Core_Reference_Interface>
2184 public static function createReferenceColumns($className) {
2186 $fields = $className::fields();
2187 foreach ($fields as $field) {
2188 if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
2189 $result[] = new CRM_Core_Reference_OptionValue(
2190 $className::getTableName(),
2192 'civicrm_option_value',
2193 CRM_Utils_Array
::value('keyColumn', $field['pseudoconstant'], 'value'),
2194 $field['pseudoconstant']['optionGroupName']
2202 * Find all records which refer to this entity.
2205 * Array of objects referencing this
2207 public function findReferences() {
2208 $links = self
::getReferencesToTable(static::getTableName());
2210 $occurrences = array();
2211 foreach ($links as $refSpec) {
2212 /** @var $refSpec CRM_Core_Reference_Interface */
2213 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($refSpec->getReferenceTable());
2214 $result = $refSpec->findReferences($this);
2216 while ($result->fetch()) {
2217 $obj = new $daoName();
2218 $obj->id
= $result->id
;
2219 $occurrences[] = $obj;
2224 return $occurrences;
2229 * each item has keys:
2233 * - table: string|null SQL table name
2234 * - key: string|null SQL column name
2236 public function getReferenceCounts() {
2237 $links = self
::getReferencesToTable(static::getTableName());
2240 foreach ($links as $refSpec) {
2241 /** @var $refSpec CRM_Core_Reference_Interface */
2242 $count = $refSpec->getReferenceCount($this);
2243 if ($count['count'] != 0) {
2248 foreach (CRM_Core_Component
::getEnabledComponents() as $component) {
2249 /** @var $component CRM_Core_Component_Info */
2250 $counts = array_merge($counts, $component->getReferenceCounts($this));
2252 CRM_Utils_Hook
::referenceCounts($this, $counts);
2258 * List all tables which have hard foreign keys to this table.
2260 * For now, this returns a description of every entity_id/entity_table
2262 * TODO: filter dynamic entity references on the $tableName, based on
2263 * schema metadata in dynamicForeignKey which enumerates a restricted
2264 * set of possible entity_table's.
2266 * @param string $tableName
2267 * Table referred to.
2270 * structure of table and column, listing every table with a
2271 * foreign key reference to $tableName, and the column where the key appears.
2273 public static function getReferencesToTable($tableName) {
2274 $refsFound = array();
2275 foreach (CRM_Core_DAO_AllCoreTables
::getClasses() as $daoClassName) {
2276 $links = $daoClassName::getReferenceColumns();
2277 $daoTableName = $daoClassName::getTableName();
2279 foreach ($links as $refSpec) {
2280 /** @var $refSpec CRM_Core_Reference_Interface */
2281 if ($refSpec->matchesTargetTable($tableName)) {
2282 $refsFound[] = $refSpec;
2290 * Get all references to contact table.
2292 * This includes core tables, custom group tables, tables added by the merge
2293 * hook and the entity_tag table.
2295 * Refer to CRM-17454 for information on the danger of querying the information
2296 * schema to derive this.
2298 public static function getReferencesToContactTable() {
2299 if (isset(\Civi
::$statics[__CLASS__
]) && isset(\Civi
::$statics[__CLASS__
]['contact_references'])) {
2300 return \Civi
::$statics[__CLASS__
]['contact_references'];
2302 $contactReferences = [];
2303 $coreReferences = CRM_Core_DAO
::getReferencesToTable('civicrm_contact');
2304 foreach ($coreReferences as $coreReference) {
2305 if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
2306 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
2309 self
::appendCustomTablesExtendingContacts($contactReferences);
2311 // FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
2312 $contactReferences['civicrm_entity_tag'][] = 'entity_id';
2313 \Civi
::$statics[__CLASS__
]['contact_references'] = $contactReferences;
2314 return \Civi
::$statics[__CLASS__
]['contact_references'];
2318 * Add custom tables that extend contacts to the list of contact references.
2320 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
2321 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
2322 * - the down side is it is not cached.
2324 * Further changes should be include tests in the CRM_Core_MergerTest class
2325 * to ensure that disabled, subtype, multiple etc groups are still captured.
2327 * @param array $cidRefs
2329 public static function appendCustomTablesExtendingContacts(&$cidRefs) {
2330 $customValueTables = CRM_Core_BAO_CustomGroup
::getAllCustomGroupsByBaseEntity('Contact');
2331 $customValueTables->find();
2332 while ($customValueTables->fetch()) {
2333 $cidRefs[$customValueTables->table_name
] = array('entity_id');
2338 * Lookup the value of a MySQL global configuration variable.
2340 * @param string $name
2341 * E.g. "thread_stack".
2342 * @param mixed $default
2345 public static function getGlobalSetting($name, $default = NULL) {
2346 // Alternatively, SELECT @@GLOBAL.thread_stack, but
2347 // that has been reported to fail under MySQL 5.0 for OS X
2348 $escapedName = self
::escapeString($name);
2349 $dao = CRM_Core_DAO
::executeQuery("SHOW VARIABLES LIKE '$escapedName'");
2350 if ($dao->fetch()) {
2360 * Update the fields array to also hold keys for pseudoconstant fields that relate to contained fields.
2362 * This is relevant where we want to offer both the ID field and the label field
2363 * as an option, e.g. search builder.
2365 * It is currently limited for optionGroupName for purposes keeping the scope of the
2366 * change small, but is appropriate for other sorts of pseudoconstants.
2368 * @param array $fields
2370 protected static function appendPseudoConstantsToFields(&$fields) {
2371 foreach ($fields as $field) {
2372 if (!empty($field['pseudoconstant']) && !empty($field['pseudoconstant']['optionGroupName'])) {
2373 $fields[$field['pseudoconstant']['optionGroupName']] = array(
2374 'title' => CRM_Core_BAO_OptionGroup
::getTitleByName($field['pseudoconstant']['optionGroupName']),
2375 'name' => $field['pseudoconstant']['optionGroupName'],
2376 'data_type' => CRM_Utils_Type
::T_STRING
,
2383 * Get options for the called BAO object's field.
2385 * This function can be overridden by each BAO to add more logic related to context.
2386 * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
2388 * @param string $fieldName
2389 * @param string $context
2390 * @see CRM_Core_DAO::buildOptionsContext
2391 * @param array $props
2392 * whatever is known about this bao object.
2394 * @return array|bool
2396 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
2397 // If a given bao does not override this function
2398 $baoName = get_called_class();
2399 return CRM_Core_PseudoConstant
::get($baoName, $fieldName, $props, $context);
2403 * Populate option labels for this object's fields.
2405 * @throws exception if called directly on the base class
2407 public function getOptionLabels() {
2408 $fields = $this->fields();
2409 if ($fields === NULL) {
2410 throw new Exception('Cannot call getOptionLabels on CRM_Core_DAO');
2412 foreach ($fields as $field) {
2413 $name = CRM_Utils_Array
::value('name', $field);
2414 if ($name && isset($this->$name)) {
2415 $label = CRM_Core_PseudoConstant
::getLabel(get_class($this), $name, $this->$name);
2416 if ($label !== FALSE) {
2417 // Append 'label' onto the field name
2418 $labelName = $name . '_label';
2419 $this->$labelName = $label;
2426 * Provides documentation and validation for the buildOptions $context param
2428 * @param string $context
2433 public static function buildOptionsContext($context = NULL) {
2435 'get' => "get: all options are returned, even if they are disabled; labels are translated.",
2436 'create' => "create: options are filtered appropriately for the object being created/updated; labels are translated.",
2437 'search' => "search: searchable options are returned; labels are translated.",
2438 'validate' => "validate: all options are returned, even if they are disabled; machine names are used in place of labels.",
2439 'abbreviate' => "abbreviate: enabled options are returned; labels are replaced with abbreviations.",
2440 'match' => "match: enabled options are returned using machine names as keys; labels are translated.",
2442 // Validation: enforce uniformity of this param
2443 if ($context !== NULL && !isset($contexts[$context])) {
2444 throw new Exception("'$context' is not a valid context for buildOptions.");
2450 * @param string $fieldName
2451 * @return bool|array
2453 public function getFieldSpec($fieldName) {
2454 $fields = $this->fields();
2455 $fieldKeys = $this->fieldKeys();
2457 // Support "unique names" as well as sql names
2458 $fieldKey = $fieldName;
2459 if (empty($fields[$fieldKey])) {
2460 $fieldKey = CRM_Utils_Array
::value($fieldName, $fieldKeys);
2462 // If neither worked then this field doesn't exist. Return false.
2463 if (empty($fields[$fieldKey])) {
2466 return $fields[$fieldKey];
2470 * Get SQL where clause for SQL filter syntax input parameters.
2472 * SQL version of api function to assign filters to the DAO based on the syntax
2473 * $field => array('IN' => array(4,6,9))
2475 * $field => array('LIKE' => array('%me%))
2478 * @param string $fieldName
2480 * @param array $filter
2481 * filter to be applied indexed by operator.
2482 * @param string $type
2483 * type of field (not actually used - nor in api @todo ).
2484 * @param string $alias
2485 * alternative field name ('as') @todo- not actually used.
2486 * @param bool $returnSanitisedArray
2487 * Return a sanitised array instead of a clause.
2488 * this is primarily so we can add filters @ the api level to the Query object based fields
2492 * @return NULL|string|array
2493 * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
2494 * depending on whether it is supported as yet
2496 public static function createSQLFilter($fieldName, $filter, $type = NULL, $alias = NULL, $returnSanitisedArray = FALSE) {
2497 foreach ($filter as $operator => $criteria) {
2498 if (in_array($operator, self
::acceptedSQLOperators(), TRUE)) {
2499 switch ($operator) {
2503 if (!$returnSanitisedArray) {
2504 return (sprintf('%s %s', $fieldName, $operator));
2507 return (sprintf('%s %s ', $fieldName, $operator));
2511 // ternary operators
2514 if (empty($criteria[0]) ||
empty($criteria[1])) {
2515 throw new Exception("invalid criteria for $operator");
2517 if (!$returnSanitisedArray) {
2518 return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO
::escapeString($criteria[0]), CRM_Core_DAO
::escapeString($criteria[1])));
2521 return NULL; // not yet implemented (tests required to implement)
2528 if (empty($criteria)) {
2529 throw new Exception("invalid criteria for $operator");
2531 $escapedCriteria = array_map(array(
2535 if (!$returnSanitisedArray) {
2536 return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria)));
2538 return $escapedCriteria;
2543 if (!$returnSanitisedArray) {
2544 return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO
::escapeString($criteria)));
2547 return NULL; // not yet implemented (tests required to implement)
2555 * @see http://issues.civicrm.org/jira/browse/CRM-9150
2556 * support for other syntaxes is discussed in ticket but being put off for now
2559 public static function acceptedSQLOperators() {
2580 * SQL has a limit of 64 characters on various names:
2581 * table name, trigger name, column name ...
2583 * For custom groups and fields we generated names from user entered input
2584 * which can be longer than this length, this function helps with creating
2585 * strings that meet various criteria.
2587 * @param string $string
2588 * The string to be shortened.
2589 * @param int $length
2590 * The max length of the string.
2592 * @param bool $makeRandom
2596 public static function shortenSQLName($string, $length = 60, $makeRandom = FALSE) {
2597 // early return for strings that meet the requirements
2598 if (strlen($string) <= $length) {
2602 // easy return for calls that dont need a randomized uniq string
2604 return substr($string, 0, $length);
2607 // the string is longer than the length and we need a uniq string
2608 // for the same tablename we need the same uniq string every time
2609 // hence we use md5 on the string, which is not random
2610 // we'll append 8 characters to the end of the tableName
2611 $md5string = substr(md5($string), 0, 8);
2612 return substr($string, 0, $length - 8) . "_{$md5string}";
2616 * https://issues.civicrm.org/jira/browse/CRM-17748
2617 * Sets the internal options to be used on a query
2619 * @param array $options
2622 public function setOptions($options) {
2623 if (is_array($options)) {
2624 $this->_options
= $options;
2629 * https://issues.civicrm.org/jira/browse/CRM-17748
2630 * wrapper to pass internal DAO options down to DB_mysql/DB_Common level
2632 * @param array $options
2635 protected function _setDBOptions($options) {
2636 global $_DB_DATAOBJECT;
2638 if (is_array($options) && count($options)) {
2639 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
2640 foreach ($options as $option_name => $option_value) {
2641 $conn->setOption($option_name, $option_value);
2648 * @param array $params
2650 public function setApiFilter(&$params) {
2654 * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity
2656 * Return format is in the form of fieldname => clauses starting with an operator. e.g.:
2659 * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)')
2663 * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values.
2667 public function addSelectWhereClause() {
2669 $fields = $this->fields();
2670 foreach ($fields as $fieldName => $field) {
2671 // Clause for contact-related entities like Email, Relationship, etc.
2672 if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array
::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') {
2673 $clauses[$fieldName] = CRM_Utils_SQL
::mergeSubquery('Contact');
2675 // Clause for an entity_table/entity_id combo
2676 if ($fieldName == 'entity_id' && isset($fields['entity_table'])) {
2677 $relatedClauses = array();
2678 $relatedEntities = $this->buildOptions('entity_table', 'get');
2679 foreach ((array) $relatedEntities as $table => $ent) {
2681 $ent = CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($table));
2682 $subquery = CRM_Utils_SQL
::mergeSubquery($ent);
2684 $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
2687 $relatedClauses[] = "(entity_table = '$table')";
2691 if ($relatedClauses) {
2692 $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))';
2696 CRM_Utils_Hook
::selectWhereClause($this, $clauses);
2701 * This returns the final permissioned query string for this entity
2703 * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
2705 * @param string $tableAlias
2708 public static function getSelectWhereClause($tableAlias = NULL) {
2709 $bao = new static();
2710 if ($tableAlias === NULL) {
2711 $tableAlias = $bao->tableName();
2714 foreach ((array) $bao->addSelectWhereClause() as $field => $vals) {
2715 $clauses[$field] = NULL;
2717 $clauses[$field] = "(`$tableAlias`.`$field` IS NULL OR (`$tableAlias`.`$field` " . implode(" AND `$tableAlias`.`$field` ", (array) $vals) . '))';
2724 * ensure database name is 'safe', i.e. only contains word characters (includes underscores)
2725 * and dashes, and contains at least one [a-z] case insenstive.
2731 public static function requireSafeDBName($database) {
2734 "/^[\w\-]*[a-z]+[\w\-]*$/i",
2738 if (empty($matches)) {
2745 * Transform an array to a serialized string for database storage.
2747 * @param array|NULL $value
2748 * @param $serializationType
2749 * @return string|NULL
2750 * @throws \Exception
2752 public static function serializeField($value, $serializationType) {
2753 if ($value === NULL) {
2756 switch ($serializationType) {
2757 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
2758 return $value === array() ?
'' : CRM_Utils_Array
::implodePadded($value);
2760 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
2761 return is_array($value) ?
implode(self
::VALUE_SEPARATOR
, $value) : $value;
2763 case self
::SERIALIZE_JSON
:
2764 return is_array($value) ?
json_encode($value) : $value;
2766 case self
::SERIALIZE_PHP
:
2767 return is_array($value) ?
serialize($value) : $value;
2769 case self
::SERIALIZE_COMMA
:
2770 return is_array($value) ?
implode(',', $value) : $value;
2773 throw new Exception('Unknown serialization method for field.');
2778 * Transform a serialized string from the database into an array.
2780 * @param string|null $value
2781 * @param $serializationType
2782 * @return array|null
2783 * @throws \Exception
2785 public static function unSerializeField($value, $serializationType) {
2786 if ($value === NULL) {
2789 if ($value === '') {
2792 switch ($serializationType) {
2793 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
2794 return (array) CRM_Utils_Array
::explodePadded($value);
2796 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
2797 return explode(self
::VALUE_SEPARATOR
, trim($value));
2799 case self
::SERIALIZE_JSON
:
2800 return strlen($value) ?
json_decode($value, TRUE) : array();
2802 case self
::SERIALIZE_PHP
:
2803 return strlen($value) ?
unserialize($value) : array();
2805 case self
::SERIALIZE_COMMA
:
2806 return explode(',', trim(str_replace(', ', '', $value)));
2809 throw new Exception('Unknown serialization method for field.');
2814 * Get exportable fields with pseudoconstants rendered as an extra field.
2816 * @param string $baoClass
2820 public static function getExportableFieldsWithPseudoConstants($baoClass) {
2821 if (method_exists($baoClass, 'exportableFields')) {
2822 $fields = $baoClass::exportableFields();
2825 $fields = $baoClass::export();
2827 CRM_Core_DAO
::appendPseudoConstantsToFields($fields);