gearbox-solutions / eloquent-filemaker

A Model extension and Eloquent driver for Laravel connecting to FileMaker through the Data API
https://gearboxgo.com
MIT License
54 stars 16 forks source link

Using == operator with null value in where query leads to exception #65

Closed lgebing closed 5 months ago

lgebing commented 5 months ago

Dependencies

Description of the issue:

Since updating to 2.x.x, when making a query like the following, where $description is equal to null, I get an error saying Illegal operator and value combination.

$description = null;

Model::query()
    ->where('description', '==', $description)
    ->get();

The exception gets thrown while evaluating value & operator in prepareValueAndOperator‎() of the FMBaseBuilder class.

// Here we will make some assumptions about the operator. If only 2 values are
// passed to the method, we will assume that the operator is an equals sign
// and keep going. Otherwise, we'll require the operator to be passed in.
[$value, $operator] = $this->prepareValueAndOperator(
    $value, $operator, func_num_args() === 2
);

Here $this->invalidOperatorAndValue($operator, $value) gets called:

public function prepareValueAndOperator($value, $operator, $useDefault = false)
{
    if ($useDefault) {
        return [$operator, ''];
    } elseif ($this->invalidOperatorAndValue($operator, $value)) {
        throw new InvalidArgumentException('Illegal operator and value combination.');
    }

    return [$value, $operator];
}

The method is not present in the FMBaseBuilder class, but in Illuminate\Database\Query\Builder:

protected function invalidOperatorAndValue($operator, $value)
{
    return is_null($value) && in_array($operator, $this->operators) && ! in_array($operator, ['=', '<>', '!=']);
}

Problem here are the hardcoded operator values, which don't include the FileMaker specific ==.

I guess this hasn't been a problem prior to version 2.x.x because of the different handling of empty values introduced there, though I'm not completely sure. However I think those changes shouldn't really matter, since from my understanding they revealed the underlying issue of the == operator not being accounted for.

It would probably be easiest to overwrite invalidOperatorAndValue() inside of the FMBaseBuilder class and add the missing value.

Smef commented 5 months ago

We'll take a look at this.

One thing you can probably do in the mean time is append the operator and your value, and don't use the operator parameter. Operators like that aren't really a thing in FileMaker, and they end up getting concatenated down the line anyway.

Does concatenating the operator and value allow that to go through successfully for you?

lgebing commented 5 months ago

That does work, thanks.

Yeah, I'm not really familiar with FileMaker itself, though I have noticed the operator being concatenated to the value in the past. Just didn't think of that here.

I'm closing this then. If something comes out of it, great. If not, no worries ✌️

Smef commented 5 months ago

This is resolved in version 2.0.2