Problem/Motivation

When selecting an integer as value for a property path, and the property path returns nul instead of always an integer, there is a warning shown :

Warning: A non-numeric value encountered in Drupal\search_api_db\Plugin\search_api\backend\Database->convert() (regel 1478 van /var/www/html/web/modules/contrib/search_api/modules/search_api_db/src/Plugin/search_api/backend/Database.php) #0 /var/www/html/web/core/includes/bootstrap.inc(582): _drupal_error_handler_real(2, 'A non-numeric v...', '/var/www/html/w...', 1478, Array) #1 /var/www/html/web/modules/contrib/search_api/modules/search_api_db/src/Plugin/search_api/backend/Database.php(1478): _drupal_error_handler(2, 'A non-numeric v...', '/var/www/html/w...', 1478, Array) #2 /var/www/html/web/modules/contrib/search_api/modules/search_api_db/src/Plugin/search_api/backend/Database.php(1227): Drupal\search_api_db\Plugin\search_api\backend\Database->convert('', 'integer', 'list', Object(Drupal\search_api\Entity\Index)) #3 /var/www/html/web/modules/contrib/search_api/modules/search_api_db/src/Plugin/search_api/backend/Database.php(1153): Drupal\search_api_db\Plugin\search_api\backend\Database->indexItem(Object(Drupal\search_api\Entity\Index), Object(Drupal\search_api\Item\Item)) #4 /var/www/html/web/modules/contrib/search_api/src/Entity/Server.php(338): Drupal\search_api_db\Plugin\search_api\backend\Database->indexItems(Object(Drupal\search_api\Entity\Index), Array) #5 /var/www/html/web/modules/contrib/search_api/src/Entity/Index.php(980): Drupal\search_api\Entity\Server->indexItems(Object(Drupal\search_api\Entity\Index), Array) #6 /var/www/html/web/modules/contrib/search_api/src/Entity/Index.php(920): Drupal\search_api\Entity\Index->indexSpecificItems(Array) #7 /var/www/html/web/modules/contrib/search_api/search_api.module(111): Drupal\search_api\Entity\Index->indexItems(50) #8 [internal function]: search_api_cron() #9 /var/www/html/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(391): call_user_func_array('search_api_cron', Array) #10 /var/www/html/web/core/lib/Drupal/Core/Cron.php(235): Drupal\Core\Extension\ModuleHandler->invoke('search_api', 'cron') #11 /var/www/html/web/core/lib/Drupal/Core/Cron.php(133): Drupal\Core\Cron->invokeCronHandlers() #12 /var/www/html/web/core/lib/Drupal/Core/ProxyClass/Cron.php(75): Drupal\Core\Cron->run() #13 /var/www/html/web/core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php(65): Drupal\Core\ProxyClass\Cron->run() #14 [internal function]: Drupal\automated_cron\EventSubscriber\AutomatedCron->onTerminate(Object(Symfony\Component\HttpKernel\Event\PostResponseEvent), 'kernel.terminat...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #15 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\PostResponseEvent), 'kernel.terminat...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #16 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(88): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.terminat...', Object(Symfony\Component\HttpKernel\Event\PostResponseEvent)) #17 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(32): Symfony\Component\HttpKernel\HttpKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Render\HtmlResponse)) #18 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(644): Stack\StackedHttpKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Render\HtmlResponse)) #19 /var/www/html/web/index.php(22): Drupal\Core\DrupalKernel->terminate(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Render\HtmlResponse)) #20 {main}.

Proposed resolution

Always check for integer of the value, even if you expect it to be an integer.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

tim-diels created an issue. See original summary.

tim-diels’s picture

StatusFileSize
new567 bytes

Patch provided.

tim-diels’s picture

Status: Active » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This only happens in an edge-case, so we don't need additional testcoverage for this, let's get this in!

drunken monkey’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new1.41 KB
new1.41 KB

It doesn't seem possible that the value is actually NULL, as we specifically check for that at the top of the method.
However, it seems the error actually triggers (at least for newer PHP versions, I'd guess) even with normal strings, if they don't represent a number. While the 0 + $value was actually meant to work around that problem, it doesn't seem to work anymore, we do need the explicit cast.

However, the 0 + part then becomes pointless, I think. Also, we want the same treatment for dates and, with different type cast, floats.
Finally, it seems we've dragged along type names from D7 which never made it into D8 – even though it's not completely “proper”, we should remove them here as well, while we're at it.

Revised patch attached, please test/review!

drunken monkey’s picture

Component: General code » Database backend
Status: Needs work » Needs review
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

I just checked with @tim-diels - The project this was happening in has seen some major refactoring and this is no longer happening - so we can't test it on the same install that had that problem.

In any case, this patch does improve the current state, both of the documentation and the actual code. Setting this back to RTBC.

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Alright, thanks for checking back, and for reviewing!
Committed.
Thanks again, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.