Merge pull request #22992 from eileenmcnaughton/billingnot
[civicrm-core.git] / Civi / Api4 / Generic / Traits / EntityBridge.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\Api4\Generic\Traits;
13
14 /**
15 * A bridge is a small table that provides an intermediary link between two other tables.
16 *
17 * The API can automatically incorporate a Bridge into a join expression.
18 */
19 trait EntityBridge {
20
21 /**
22 * Adds "bridge" info, which should specify an array of two field names from this entity
23 *
24 * This automatic function can be overridden by adding a getInfo function to the api entity class.
25 *
26 * @return array
27 */
28 public static function getInfo() {
29 $info = parent::getInfo();
30 $bridgeFields = [];
31 if (!empty($info['dao'])) {
32 foreach (($info['dao'])::fields() as $field) {
33 if (!empty($field['FKClassName']) || $field['name'] === 'entity_id') {
34 $bridgeFields[] = $field['name'];
35 }
36 }
37 }
38 if (count($bridgeFields) === 2) {
39 $info['bridge'] = [
40 $bridgeFields[0] => ['to' => $bridgeFields[1]],
41 $bridgeFields[1] => ['to' => $bridgeFields[0]],
42 ];
43 }
44 return $info;
45 }
46
47 }