From c16b4b6b1346b360e428476658c638134ce13639 Mon Sep 17 00:00:00 2001 From: Coleman Watts <coleman@civicrm.org> Date: Wed, 6 May 2020 11:33:09 -0400 Subject: [PATCH] APIv4 - Remove optionValue joins OptionValue joins were deprecated in favor of pseudoconstant suffixes. This removes them. --- Civi/Api4/Query/Api4SelectQuery.php | 4 - .../Schema/Joinable/OptionValueJoinable.php | 80 ------------------- Civi/Api4/Service/Schema/SchemaMapBuilder.php | 46 ----------- tests/phpunit/api/v4/Action/FkJoinTest.php | 2 +- .../Query/Api4SelectQueryComplexJoinTest.php | 8 +- 5 files changed, 5 insertions(+), 135 deletions(-) delete mode 100644 Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php diff --git a/Civi/Api4/Query/Api4SelectQuery.php b/Civi/Api4/Query/Api4SelectQuery.php index d7e6da44c8..63cba4e280 100644 --- a/Civi/Api4/Query/Api4SelectQuery.php +++ b/Civi/Api4/Query/Api4SelectQuery.php @@ -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 index 6b9b539faa..0000000000 --- a/Civi/Api4/Service/Schema/Joinable/OptionValueJoinable.php +++ /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; - } - -} diff --git a/Civi/Api4/Service/Schema/SchemaMapBuilder.php b/Civi/Api4/Service/Schema/SchemaMapBuilder.php index 74dc0f3453..684784f54f 100644 --- a/Civi/Api4/Service/Schema/SchemaMapBuilder.php +++ b/Civi/Api4/Service/Schema/SchemaMapBuilder.php @@ -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; diff --git a/tests/phpunit/api/v4/Action/FkJoinTest.php b/tests/phpunit/api/v4/Action/FkJoinTest.php index babd1cec0f..66e2c9dfe6 100644 --- a/tests/phpunit/api/v4/Action/FkJoinTest.php +++ b/tests/phpunit/api/v4/Action/FkJoinTest.php @@ -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(); diff --git a/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php b/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php index e51ed6b49e..eaae446b40 100644 --- a/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php +++ b/tests/phpunit/api/v4/Query/Api4SelectQueryComplexJoinTest.php @@ -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']); } } -- 2.25.1