APIv4 - Remove optionValue joins
authorColeman Watts <coleman@civicrm.org>
Wed, 6 May 2020 15:33:09 +0000 (11:33 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 15 May 2020 16:02:06 +0000 (12:02 -0400)
OptionValue joins were deprecated in favor of pseudoconstant suffixes. This removes them.

Civi/Api4/Query/Api4SelectQuery.php
Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php [deleted file]
Civi/Api4/Service/Schema/SchemaMapBuilder.php
tests/phpunit/api/v4/Action/FkJoinTest.php
tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php

index d7e6da44c8138e75ead7e3248d389dce45ee1be1..63cba4e2807b06bbf60e3a25e904177da3ea6841 100644 (file)
@@ -16,7 +16,6 @@ use Civi\Api4\Event\Events;
 use Civi\Api4\Event\PostSelectQueryEvent;
 use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
 use Civi\Api4\Service\Schema\Joinable\Joinable;
-use Civi\Api4\Service\Schema\Joinable\OptionValueJoinable;
 use Civi\Api4\Utils\FormattingUtil;
 use Civi\Api4\Utils\CoreUtil;
 use Civi\Api4\Utils\SelectUtil;
@@ -430,9 +429,6 @@ class Api4SelectQuery extends SelectQuery {
       if ($joinable->getJoinType() === Joinable::JOIN_TYPE_ONE_TO_MANY) {
         $isMany = TRUE;
       }
-      if ($joinable instanceof OptionValueJoinable) {
-        \Civi::log()->warning('Use API pseudoconstant suffix like :name or :label instead of join.', ['civi.tag' => 'deprecated']);
-      }
     }
 
     /** @var \Civi\Api4\Service\Schema\Joinable\Joinable $lastLink */
diff --git a/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php b/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php
deleted file mode 100644 (file)
index 6b9b539..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-
-/*
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC. All rights reserved.                        |
- |                                                                    |
- | This work is published under the GNU AGPLv3 license with some      |
- | permitted exceptions and without any warranty. For full license    |
- | and copyright information, see https://civicrm.org/licensing       |
- +--------------------------------------------------------------------+
- */
-
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
- */
-
-
-namespace Civi\Api4\Service\Schema\Joinable;
-
-class OptionValueJoinable extends Joinable {
-  /**
-   * @var string
-   */
-  protected $optionGroupName;
-
-  /**
-   * @param string $optionGroup
-   *   Can be either the option group name or ID
-   * @param string|null $alias
-   *   The join alias
-   * @param string $keyColumn
-   *   Which column to use to join, defaults to "value"
-   */
-  public function __construct($optionGroup, $alias = NULL, $keyColumn = 'value') {
-    $this->optionGroupName = $optionGroup;
-    $optionValueTable = 'civicrm_option_value';
-
-    // default join alias to option group name, e.g. activity_type
-    if (!$alias && !is_numeric($optionGroup)) {
-      $alias = $optionGroup;
-    }
-
-    parent::__construct($optionValueTable, $keyColumn, $alias);
-
-    if (!is_numeric($optionGroup)) {
-      $subSelect = 'SELECT id FROM civicrm_option_group WHERE name = "%s"';
-      $subQuery = sprintf($subSelect, $optionGroup);
-      $condition = sprintf('%s.option_group_id = (%s)', $alias, $subQuery);
-    }
-    else {
-      $condition = sprintf('%s.option_group_id = %d', $alias, $optionGroup);
-    }
-
-    $this->addCondition($condition);
-  }
-
-  /**
-   * The existing condition must also be re-aliased
-   *
-   * @param string $alias
-   *
-   * @return $this
-   */
-  public function setAlias($alias) {
-    foreach ($this->conditions as $index => $condition) {
-      $search = $this->alias . '.';
-      $replace = $alias . '.';
-      $this->conditions[$index] = str_replace($search, $replace, $condition);
-    }
-
-    parent::setAlias($alias);
-
-    return $this;
-  }
-
-}
index 74dc0f34536906561efd7fc71539554f9c8bda1c..684784f54fc3a97802897a523148ac7ebdf97e56 100644 (file)
@@ -27,7 +27,6 @@ use Civi\Api4\Event\SchemaMapBuildEvent;
 use Civi\Api4\Service\Schema\Joinable\CustomGroupJoinable;
 use Civi\Api4\Service\Schema\Joinable\Joinable;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
-use Civi\Api4\Service\Schema\Joinable\OptionValueJoinable;
 use CRM_Core_DAO_AllCoreTables as AllCoreTables;
 
 class SchemaMapBuilder {
@@ -99,41 +98,6 @@ class SchemaMapBuilder {
       $joinable->setJoinType($joinable::JOIN_TYPE_MANY_TO_ONE);
       $table->addTableLink($field, $joinable);
     }
-    elseif (!empty($data['pseudoconstant'])) {
-      $this->addPseudoConstantJoin($table, $field, $data);
-    }
-  }
-
-  /**
-   * @param Table $table
-   * @param string $field
-   * @param array $data
-   */
-  private function addPseudoConstantJoin(Table $table, $field, array $data) {
-    $pseudoConstant = $data['pseudoconstant'] ?? NULL;
-    $tableName = $pseudoConstant['table'] ?? NULL;
-    $optionGroupName = $pseudoConstant['optionGroupName'] ?? NULL;
-    $keyColumn = $pseudoConstant['keyColumn'] ?? 'id';
-
-    if ($tableName) {
-      $alias = str_replace('civicrm_', '', $tableName);
-      $joinable = new Joinable($tableName, $keyColumn, $alias);
-      $condition = $pseudoConstant['condition'] ?? NULL;
-      if ($condition) {
-        $joinable->addCondition($condition);
-      }
-      $table->addTableLink($field, $joinable);
-    }
-    elseif ($optionGroupName) {
-      $keyColumn = $pseudoConstant['keyColumn'] ?? 'value';
-      $joinable = new OptionValueJoinable($optionGroupName, NULL, $keyColumn);
-
-      if (!empty($data['serialize'])) {
-        $joinable->setJoinType($joinable::JOIN_TYPE_ONE_TO_MANY);
-      }
-
-      $table->addTableLink($field, $joinable);
-    }
   }
 
   /**
@@ -144,11 +108,6 @@ class SchemaMapBuilder {
   private function addBackReferences(SchemaMap $map) {
     foreach ($map->getTables() as $table) {
       foreach ($table->getTableLinks() as $link) {
-        // there are too many possible joins from option value so skip
-        if ($link instanceof OptionValueJoinable) {
-          continue;
-        }
-
         $target = $map->getTableByName($link->getTargetTable());
         $tableName = $link->getBaseTable();
         $plural = str_replace('civicrm_', '', $this->getPlural($tableName));
@@ -213,11 +172,6 @@ class SchemaMapBuilder {
         $customTable = new Table($tableName);
       }
 
-      if (!empty($fieldData->option_group_id)) {
-        $optionValueJoinable = new OptionValueJoinable($fieldData->option_group_id, $fieldData->label);
-        $customTable->addTableLink($fieldData->column_name, $optionValueJoinable);
-      }
-
       $map->addTable($customTable);
 
       $alias = $fieldData->custom_group_name;
index babd1cec0fabeb1dda14764ab839aef72eefa605..66e2c9dfe6ee67e063f7f7bd25f512e68a498e94 100644 (file)
@@ -81,7 +81,7 @@ class FkJoinTest extends UnitTestCase {
       ->setCheckPermissions(FALSE)
       ->addSelect('phones.phone')
       ->addWhere('id', '=', $testContact['id'])
-      ->addWhere('phones.location_type.name', '=', 'Home')
+      ->addWhere('phones.location_type_id:name', '=', 'Home')
       ->execute()
       ->first();
 
index e51ed6b49e059d443c0dbf1f92c587d5d6a1a10b..eaae446b40279c55a277afb4c48467e59bc07f8a 100644 (file)
@@ -51,7 +51,7 @@ class Api4SelectQueryComplexJoinTest extends UnitTestCase {
     $query->select[] = 'display_name';
     $query->select[] = 'phones.*_id';
     $query->select[] = 'emails.email';
-    $query->select[] = 'emails.location_type.name';
+    $query->select[] = 'emails.location_type_id:name';
     $query->select[] = 'created_activities.contact_id';
     $query->select[] = 'created_activities.activity.subject';
     $query->select[] = 'created_activities.activity.activity_type_id:name';
@@ -74,7 +74,7 @@ class Api4SelectQueryComplexJoinTest extends UnitTestCase {
     $this->assertContains($firstActivity['subject'], $activitySubjects);
     $this->assertArrayHasKey('activity_type_id:name', $firstActivity);
 
-    $this->assertArrayHasKey('name', $firstResult['emails'][0]['location_type']);
+    $this->assertArrayHasKey('location_type_id:name', $firstResult['emails'][0]);
     $this->assertArrayHasKey('location_type_id', $firstResult['phones'][0]);
     $this->assertArrayHasKey('id', $firstResult['phones'][0]);
     $this->assertArrayNotHasKey('phone', $firstResult['phones'][0]);
@@ -98,13 +98,13 @@ class Api4SelectQueryComplexJoinTest extends UnitTestCase {
     $query->select[] = 'id';
     $query->select[] = 'first_name';
     // before emails selection
-    $query->select[] = 'emails.location_type.name';
+    $query->select[] = 'emails.location_type_id:name';
     $query->select[] = 'emails.email';
     $query->where[] = ['emails.email', 'IS NOT NULL'];
     $results = $query->run();
     $firstResult = array_shift($results);
 
-    $this->assertNotEmpty($firstResult['emails'][0]['location_type']['name']);
+    $this->assertNotEmpty($firstResult['emails'][0]['location_type_id:name']);
   }
 
 }