Skip to content

Commit

Permalink
Add primaryKey info to daos
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jul 2, 2021
1 parent 72b9f70 commit 05ab13e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CRM/Core/CodeGen/Specification.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function getTable($tableXML, &$database, &$tables) {
'comment' => $this->value('comment', $tableXML),
'description' => $this->value('description', $tableXML),
'localizable' => $localizable,
'primary_key' => (array) $tableXML->primaryKey->name,
'log' => $this->value('log', $tableXML, 'false'),
'archive' => $this->value('archive', $tableXML, 'false'),
];
Expand Down
7 changes: 7 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
*/
class CRM_Core_DAO extends DB_DataObject {

/**
* Primary key field(s).
*
* @var string[]
*/
public static $_primaryKey = ['id'];

/**
* How many times has this instance been cloned.
*
Expand Down
1 change: 1 addition & 0 deletions Civi/Api4/Generic/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public static function getInfo() {
$dao = \CRM_Core_DAO_AllCoreTables::getFullName($info['name']);
if ($dao) {
$info['paths'] = $dao::getEntityPaths();
$info['primary_key'] = $dao::$_primaryKey;
$info['icon'] = $dao::$_icon;
$info['label_field'] = $dao::$_labelField;
$info['dao'] = $dao;
Expand Down
12 changes: 11 additions & 1 deletion xml/templates/dao.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ class {$table.className} extends CRM_Core_DAO {ldelim}
{if !empty($table.component)}const COMPONENT = '{$table.component}';{/if}

/**
* Static instance to hold the table name.
* Name of SQL table for the {$table.entity} entity.
*
* @var string
*/
public static $_tableName = '{$table.name}';

{* Only print this variable if it's different than the default in CRM_Core_DAO *}
{if count($table.primary_key) > 1 || $table.primary_key.0 !== 'id'}
/**
* Primary key field(s).
*
* @var string[]
*/
public static $_primaryKey = ['{"', '"|implode:$table.primary_key}'];
{/if}

{if $table.icon}
/**
* Icon associated with this entity.
Expand Down

0 comments on commit 05ab13e

Please sign in to comment.