3 namespace Civi\Api4\Service\Schema\Joinable
;
5 use Civi\Api4\Utils\CoreUtil
;
6 use CRM_Core_DAO_AllCoreTables
as AllCoreTables
;
10 const JOIN_SIDE_LEFT
= 'LEFT';
11 const JOIN_SIDE_INNER
= 'INNER';
13 const JOIN_TYPE_ONE_TO_ONE
= '1_to_1';
14 const JOIN_TYPE_MANY_TO_ONE
= 'n_to_1';
15 const JOIN_TYPE_ONE_TO_MANY
= '1_to_n';
25 protected $baseColumn;
30 protected $targetTable;
35 * Name (or alias) of the target column)
37 protected $targetColumn;
47 protected $conditions = [];
52 protected $joinSide = self
::JOIN_SIDE_LEFT
;
57 protected $joinType = self
::JOIN_TYPE_ONE_TO_ONE
;
67 protected $entityFields;
71 * @param $targetColumn
72 * @param string|null $alias
74 public function __construct($targetTable, $targetColumn, $alias = NULL) {
75 $this->targetTable
= $targetTable;
76 $this->targetColumn
= $targetColumn;
78 $this->entity
= CoreUtil
::getApiNameFromTableName($targetTable);
80 $this->alias
= $alias ?
: str_replace('civicrm_', '', $targetTable);
84 * Gets conditions required when joining to a base table
86 * @param string|null $baseTableAlias
87 * Name of the base table, if aliased.
91 public function getConditionsForJoin($baseTableAlias = NULL) {
92 $baseCondition = sprintf(
94 $baseTableAlias ?
: $this->baseTable
,
100 return array_merge([$baseCondition], $this->conditions
);
106 public function getBaseTable() {
107 return $this->baseTable
;
111 * @param string $baseTable
115 public function setBaseTable($baseTable) {
116 $this->baseTable
= $baseTable;
124 public function getBaseColumn() {
125 return $this->baseColumn
;
129 * @param string $baseColumn
133 public function setBaseColumn($baseColumn) {
134 $this->baseColumn
= $baseColumn;
142 public function getAlias() {
147 * @param string $alias
151 public function setAlias($alias) {
152 $this->alias
= $alias;
160 public function getTargetTable() {
161 return $this->targetTable
;
167 public function getTargetColumn() {
168 return $this->targetColumn
;
174 public function getEntity() {
175 return $this->entity
;
183 public function addCondition($condition) {
184 $this->conditions
[] = $condition;
192 public function getExtraJoinConditions() {
193 return $this->conditions
;
197 * @param array $conditions
201 public function setConditions($conditions) {
202 $this->conditions
= $conditions;
210 public function getJoinSide() {
211 return $this->joinSide
;
215 * @param string $joinSide
219 public function setJoinSide($joinSide) {
220 $this->joinSide
= $joinSide;
228 public function getJoinType() {
229 return $this->joinType
;
233 * @param int $joinType
237 public function setJoinType($joinType) {
238 $this->joinType
= $joinType;
246 public function toArray() {
247 return get_object_vars($this);
251 * @return \Civi\Api4\Service\Spec\FieldSpec[]
253 public function getEntityFields() {
254 if (!$this->entityFields
) {
255 $bao = AllCoreTables
::getClassForTable($this->getTargetTable());
257 foreach ($bao::fields() as $field) {
258 $this->entityFields
[] = \Civi\Api4\Service\Spec\SpecFormatter
::arrayToField($field, $this->getEntity());
262 return $this->entityFields
;
266 * @return \Civi\Api4\Service\Spec\FieldSpec|NULL
268 public function getField($fieldName) {
269 foreach ($this->getEntityFields() as $field) {
270 if ($field->getName() === $fieldName) {