Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] DAO - Consolidate redundant functions keys() and getPrimaryKey() #29367

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
DAO - Consolidate redundant functions keys() and getPrimaryKey()
The `getPrimaryKey()` function was added recently and the older one was hobbled by not returning the declared `$_primaryKey`.
Fortunately the new function was protected so could be safely removed while fixing `keys()` to do the right thing.
  • Loading branch information
colemanw committed Feb 11, 2024
commit 4bf36bbcf5d2617cc4462749781fa8a95ae1fe3a
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can't find any references to this in universe.


/**
* @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];
}

}