Page MenuHomePhorge

Allow to find objects by multiple custom field values (IN query)
Closed, ResolvedPublic

Description

Premising that you can already "easily" find objects by a custom field single value - see Q117;

It seems that you cannot do an "IN" query with multiple values.

So, this works to find objects with a value:

<?php
require 'phorge/scripts/init/init-script.php';

$field_key = 'std:maniphest:mycompany.estimated-hours';

$query_value = '221';

// TODO: un-comment to cause a nuclear crash
// $query_value = array( '221', '222', '223' );

$viewer = PhabricatorUser::getOmnipotentUser();

// Find the Field definition
$engine = new ManiphestTaskSearchEngine();
$task = new ManiphestTask();
$field = PhabricatorCustomField::getObjectField(
  $task,
  PhabricatorCustomField::ROLE_DEFAULT,
  $field_key);

if(!$field) {
	throw new Exception(pht("Cannot find Maniphest field called %s", $field_key));
}

// Query Tasks with that value.
$query = ($engine->newQuery())
  ->setViewer($viewer);

$field->applyApplicationSearchConstraintToQuery(
  $engine,
  $query,
  $query_value);

$results = $query->execute();
foreach ($results as $res) {
  echo pht("Found task with id: %d \n", $res->getID());
}

But if you uncomment the line with an array, with multiple values I get this exception:

[2024-03-07 12:13:47] EXCEPTION: (InvalidArgumentException) Call to phutil_nonempty_scalar() expected: a string; or stringlike object; or int; or float. Got: list<string>. at [<arcanist>/src/utils/utils.php:2230]
  #0 phutil_nonempty_scalar(array) called at [<phorge>/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php:56]
  ...

Maybe I'm doing it wrong, but anyway, that exception is raised here:

https://we.phorge.it/source/phorge/browse/master/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php;faf43d7edf7b2da743cc900e4ecdca10f64a973f$51-61

Previous version also affected:

https://we.phorge.it/source/phorge/browse/master/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php;821708414eecc90824810f7805cf236da3124498$51-61


But interestingly, under the hood, this thing is already supported:

https://we.phorge.it/source/phorge/browse/master/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php;faf43d7edf7b2da743cc900e4ecdca10f64a973f$1344-1358

Proposed solution

I was able to make this working with this local patch - that may have sense to discuss upstream

diff
diff --git a/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php b/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php
index 3ee42a531c..e7b42f002f 100644
--- a/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php
+++ b/src/infrastructure/customfield/standard/PhabricatorStandardCustomFieldInt.php
@@ -53,7 +53,13 @@ final class PhabricatorStandardCustomFieldInt
     PhabricatorCursorPagedPolicyAwareQuery $query,
     $value) {
 
-    if (phutil_nonempty_scalar($value)) {
+    if(is_array($value)) {
+      $is_nonempty = $value;
+    } else {
+      $is_nonempty = phutil_nonempty_scalar($value);
+    }
+
+    if ($is_nonempty) {
       $query->withApplicationSearchContainsConstraint(
         $this->newNumericIndex(null),
         $value);