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 null $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);
75 * @param CRM_Core_DAO $targetDao
79 public function findReferences($targetDao) {
80 $targetColumn = $this->getTargetKey();
82 // CRM-19385: Since id is removed, return all rows for cache tables.
83 if (!CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($this->getReferenceTable(), 'id')) {
87 1 => [$targetDao->$targetColumn, 'String'],
91 FROM {$this->getReferenceTable()}
92 WHERE {$this->getReferenceKey()} = %1
95 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($this->getReferenceTable());
96 $result = CRM_Core_DAO
::executeQuery($sql, $params, TRUE, $daoName);
101 * @param CRM_Core_DAO $targetDao
105 public function getReferenceCount($targetDao) {
106 $targetColumn = $this->getTargetKey();
108 1 => [$targetDao->$targetColumn, 'String'],
112 FROM {$this->getReferenceTable()}
113 WHERE {$this->getReferenceKey()} = %1
117 'name' => implode(':', ['sql', $this->getReferenceTable(), $this->getReferenceKey()]),
118 'type' => get_class($this),
119 'table' => $this->getReferenceTable(),
120 'key' => $this->getReferenceKey(),
121 'count' => CRM_Core_DAO
::singleValueQuery($sql, $params),