Skip to content

Commit

Permalink
Merge pull request #24584 from eileenmcnaughton/import_key
Browse files Browse the repository at this point in the history
Undo breakage of multi-primary key entities
  • Loading branch information
demeritcowboy authored Sep 23, 2022
2 parents e1dacbd + 36d748c commit 12deedc
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

use Civi\Core\DAO\Event\PostUpdate;
use Civi\Core\DAO\Event\PreUpdate;

if (!defined('DB_DSN_MODE')) {
define('DB_DSN_MODE', 'auto');
}
Expand Down Expand Up @@ -543,7 +546,9 @@ public function keys() {
public function sequenceKey() {
static $sequenceKeys;
if (!isset($sequenceKeys)) {
$sequenceKeys = [CRM_Utils_Array::single($this->getPrimaryKey()), TRUE];
// See comments in 'save' function about use of 'id' for multiple key extensions.
$key = count($this->getPrimaryKey()) > 1 ? 'id' : $this->getPrimaryKey()[0];
$sequenceKeys = [$key, TRUE];
}
return $sequenceKeys;
}
Expand Down Expand Up @@ -641,37 +646,39 @@ public function table() {
*/
public function save($hook = TRUE) {
$eventID = uniqid();
// In practice the 'Import' entities are probably the only ones with a single
// primary key that is not import. Should we check all if more than one?
// Can do that when it comes up...
$primaryField = CRM_Utils_Array::single($this->getPrimaryKey());
// Historically it was always 'id'. It is now the case that some entities (import entities)
// have a single key that is NOT 'id'. However, for entities that have multiple
// 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.
$primaryField = count($this->getPrimaryKey()) > 1 ? 'id' : $this->getPrimaryKey()[0];
if (!empty($this->$primaryField)) {
if ($hook) {
$preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
$preEvent = new PreUpdate($this);
$preEvent->eventID = $eventID;
\Civi::dispatcher()->dispatch('civi.dao.preUpdate', $preEvent);
}

$result = $this->update();

if ($hook) {
$event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
$event = new PostUpdate($this, $result);
$event->eventID = $eventID;
\Civi::dispatcher()->dispatch('civi.dao.postUpdate', $event);
}
$this->clearDbColumnValueCache();
}
else {
if ($hook) {
$preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
$preEvent = new PreUpdate($this);
$preEvent->eventID = $eventID;
\Civi::dispatcher()->dispatch("civi.dao.preInsert", $preEvent);
}

$result = $this->insert();

if ($hook) {
$event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
$event = new PostUpdate($this, $result);
$event->eventID = $eventID;
\Civi::dispatcher()->dispatch("civi.dao.postInsert", $event);
}
Expand Down

0 comments on commit 12deedc

Please sign in to comment.