Where formatter should be checking if key exists rather than the value

OS info

  • Operating System : Windows 10.0.22621
  • Wappler Version : 6.0.5
  • PHP model

Problem description

where('disabled', '===', null) is returning an empty array when it should not.

\dmxConnectLib\lib\formatters\collections.php (line 35)

This should not be checking the value. In this case, it will always continue because the value is null.
if (!isset($o[$prop])) continue;

It should be changed to check if key exists.
if (!array_key_exists($prop, $o)) continue;

Here is an API to test with/without the proposed change.

Before change, returns an empty array.
After change, returns the filtered array.

<?php
require('../../dmxConnectLib/dmxConnect.php');


$app = new \lib\App();

$app->define(<<<'JSON'
[
  {
    "name": "list",
    "module": "arraylist",
    "action": "create",
    "options": {
      "schema": [
        {
          "type": "text",
          "name": "name"
        },
        {
          "type": "boolean",
          "name": "status"
        }
      ]
    },
    "meta": [
      {
        "type": "text",
        "name": "name"
      },
      {
        "type": "boolean",
        "name": "status"
      }
    ],
    "outputType": "array"
  },
  {
    "name": "",
    "module": "arraylist",
    "action": "add",
    "options": {
      "ref": "list",
      "value": {
        "name": "active",
        "status": 1
      }
    }
  },
  {
    "name": "",
    "module": "arraylist",
    "action": "add",
    "options": {
      "ref": "list",
      "value": {
        "name": "disabled",
        "status": 1
      }
    }
  },
  {
    "name": "",
    "module": "arraylist",
    "action": "add",
    "options": {
      "ref": "list",
      "value": {
        "name": "hidden",
        "status": null
      }
    }
  },
  {
    "name": "list",
    "module": "arraylist",
    "action": "value",
    "options": {
      "ref": "list"
    },
    "output": true,
    "meta": [
      {
        "name": "name",
        "type": "text"
      },
      {
        "name": "status",
        "type": "boolean"
      }
    ],
    "outputType": "array"
  },
  {
    "name": "foo",
    "module": "core",
    "action": "setvalue",
    "options": {
      "value": "{{list.where('status', '==', null)}}"
    },
    "meta": [
      {
        "name": "name",
        "type": "text"
      },
      {
        "name": "status",
        "type": "boolean"
      }
    ],
    "outputType": "array",
    "output": true
  }
]
JSON
);
?>

The where formatter is mostly used on recordsets from a database and then it is logical to check the values and not the keys. You don’t check if a column exists but instead check if it has a value.

What is the datasource where you use the where formatter on and why would you want to check on the key?

I see your point. Then this check should be deleted as it is not possible to check for null value of a column.

What is when you check:

where('disabled', '===', undefined)

In JavaScript there is a difference between null and undefined, null is an actual value and undefined means the variable isn’t defined. So checking for undefined would be the same as checking if the key exists.

Returns an empty array. The null value continues the loop bypassing the switch statement resulting in an empty array. Please test the API I provided prior to see the issue.

Also, the 3rd parameter $value is not the issue as the comparison does not pass this point.

if (!isset($o[$prop])) continue;

Ok, I understand now what you mean, will update the code for the next update.

2 Likes

Fixed in Wappler 6.1.1

This topic was automatically closed after 2 days. New replies are no longer allowed.