4 * Description of a one-way link between two entities
6 * This is a basic SQL foreign key.
8 class CRM_Core_Reference_Basic
implements CRM_Core_Reference_Interface
{
11 protected $refTypeColumn;
12 protected $targetTable;
18 * @param string $targetTable
19 * @param string $targetKey
20 * @param null $refTypeColumn
22 public function __construct($refTable, $refKey, $targetTable = NULL, $targetKey = 'id', $refTypeColumn = NULL) {
23 $this->refTable
= $refTable;
24 $this->refKey
= $refKey;
25 $this->targetTable
= $targetTable;
26 $this->targetKey
= $targetKey;
27 $this->refTypeColumn
= $refTypeColumn;
33 public function getReferenceTable() {
34 return $this->refTable
;
40 public function getReferenceKey() {
47 public function getTypeColumn() {
48 return $this->refTypeColumn
;
54 public function getTargetTable() {
55 return $this->targetTable
;
61 public function getTargetKey() {
62 return $this->targetKey
;
66 * @param string $tableName
70 public function matchesTargetTable($tableName) {
71 return ($this->getTargetTable() === $tableName);
76 * [table_name => EntityName]
78 public function getTargetEntities(): array {
79 return [$this->targetTable
=> CRM_Core_DAO_AllCoreTables
::getEntityNameForTable($this->targetTable
)];
83 * @param CRM_Core_DAO $targetDao
87 public function findReferences($targetDao) {
88 $targetColumn = $this->getTargetKey();
90 // CRM-19385: Since id is removed, return all rows for cache tables.
91 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($this->getReferenceTable(), 'id')) {
95 1 => [$targetDao->$targetColumn, 'String'],
99 FROM {$this->getReferenceTable()}
100 WHERE {$this->getReferenceKey()} = %1
103 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($this->getReferenceTable());
104 $result = CRM_Core_DAO
::executeQuery($sql, $params, TRUE, $daoName);
109 * @param CRM_Core_DAO $targetDao
113 public function getReferenceCount($targetDao) {
114 $targetColumn = $this->getTargetKey();
116 if ($targetDao->{$targetColumn} !== '' && $targetDao->{$targetColumn} !== NULL) {
119 1 => [$targetDao->{$targetColumn} ??
'', 'String'],
123 FROM {$this->getReferenceTable()}
124 WHERE {$this->getReferenceKey()} = %1
126 $count = CRM_Core_DAO
::singleValueQuery($sql, $params);
130 'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
131 'type' => get_class($this),
132 'table' => $this->getReferenceTable(),
133 'key' => $this->getReferenceKey(),