Skip to content

Commit

Permalink
Merge pull request #29367 from colemanw/keys
Browse files Browse the repository at this point in the history
[REF] DAO - Consolidate redundant functions keys() and getPrimaryKey()
  • Loading branch information
totten authored Feb 12, 2024
2 parents f27db52 + 4bf36bb commit 56d097d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 66 deletions.
26 changes: 6 additions & 20 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ class CRM_Core_DAO extends DB_DataObject {
*/
public static $_primaryKey = ['id'];

/**
* @return string[]
*/
protected function getPrimaryKey(): array {
return static::$_primaryKey;
}

/**
* @return string
*/
Expand All @@ -58,7 +51,7 @@ protected function getFirstPrimaryKey(): string {
// keys (which we support in codegen if not many other places) we return 'id'
// simply because that is what we historically did & we don't want to 'just change'
// it & break those extensions without doing the work to create an alternative.
return count($this->getPrimaryKey()) > 1 ? 'id' : $this->getPrimaryKey()[0];
return count($this->keys()) > 1 ? 'id' : $this->keys()[0];
}

/**
Expand Down Expand Up @@ -525,31 +518,24 @@ public function initialize() {
}

/**
* Defines the default key as 'id'.
* Returns primary keys (usually ['id'])
*
* @return array
* @return string[]
*/
public function keys() {
static $keys;
if (!isset($keys)) {
$keys = ['id'];
}
return $keys;
return static::$_primaryKey;
}

/**
* Tells DB_DataObject which keys use autoincrement.
* 'id' is autoincrementing by default.
*
* FIXME: this should return all autoincrement keys not just the first.
*
* @return array
*/
public function sequenceKey() {
static $sequenceKeys;
if (!isset($sequenceKeys)) {
$sequenceKeys = [$this->getFirstPrimaryKey(), TRUE];
}
return $sequenceKeys;
return [$this->getFirstPrimaryKey(), TRUE];
}

/**
Expand Down
27 changes: 0 additions & 27 deletions ext/civiimport/Civi/BAO/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ public static function getImportTables(): array {
return _civiimport_civicrm_get_import_tables();
}

/**
* @return string[]
*/
protected function getPrimaryKey(): array {
return self::$_primaryKey;
}

/**
* Returns fields generic to all imports, indexed by name.
*
Expand Down Expand Up @@ -285,24 +278,4 @@ private static function getAllFields(string $tableName): array {
return array_merge(self::getFieldsForTable($tableName), self::getSupportedFields());
}

/**
* Defines the default key as 'id'.
*
* @return array
*/
public function keys() {
return ['_id'];
}

/**
* Tells DB_DataObject which keys use autoincrement.
* 'id' is autoincrementing by default.
*
*
* @return array
*/
public function sequenceKey() {
return ['_id', TRUE];
}

}
19 changes: 0 additions & 19 deletions ext/search_kit/Civi/BAO/SK_Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,4 @@ public static function tableHasBeenAdded(): bool {
return TRUE;
}

/**
* Defines the primary key(s).
*
* @return array
*/
public function keys() {
return ['_row'];
}

/**
* Tells DB_DataObject which keys use autoincrement.
* Overrides the default 'id'.
*
* @return array
*/
public function sequenceKey() {
return ['_row', TRUE];
}

}

0 comments on commit 56d097d

Please sign in to comment.