Skip to content

BETWEEN on relations not grouped #43

@orditeck

Description

@orditeck

Hi,

?start_date[]=(ge)2018-08-01&start_date[]=(le)2018-08-02

translates to the following MySQL query:

WHERE `start_date` >= 2018-08-01 
AND 
`start_date` <= 2018-08-02

Which is the expected result. However,

?instances:start_date[]=(ge)2018-08-01&instances:start_date[]=(le)2018-08-02

becomes

WHERE EXISTS 
( 
       SELECT * 
       FROM   `expedition_instances` 
       WHERE  `expeditions`.`id` = `expedition_instances`.`expedition_id` 
       AND    `start_date` >= '2018-08-01') 
AND 
EXISTS 
( 
       SELECT * 
       FROM   `expedition_instances` 
       WHERE  `expeditions`.`id` = `expedition_instances`.`expedition_id` 
       AND    `start_date` <= '2018-08-02')

The expected result is

WHERE EXISTS 
( 
       SELECT * 
       FROM   `expedition_instances` 
       WHERE  `expeditions`.`id` = `expedition_instances`.`expedition_id` 
       WHERE  `start_date` >= 2018-08-01 
       AND    `start_date` <= 2018-08-02 )

Feel free to change the issue's title, I wasn't able to think of any better name for it atm.

Edit 1

The constraints looks correctly grouped into instances:start_date:

/expeditions?with[]=instances&instances:start_date[]=(ge)2018-08-01&instances:start_date[]=(le)2018-08-02

array(1) {
  ["instances:start_date"]=>
  array(2) {
    [0]=>
    object(Jedrzej\Searchable\Constraint)#715 (3) {
      ["operator":protected]=>
      string(2) ">="
      ["value":protected]=>
      string(10) "2018-08-01"
      ["is_negation":protected]=>
      bool(false)
    }
    [1]=>
    object(Jedrzej\Searchable\Constraint)#716 (3) {
      ["operator":protected]=>
      string(2) "<="
      ["value":protected]=>
      string(10) "2018-08-02"
      ["is_negation":protected]=>
      bool(false)
    }
  }
}

Edit 2

I think that all constraints of the group should be applied when doing

$builder->whereHas($relation, function (Builder $builder) use ($field, $mode) {
    $this->doApply($builder, $field, $mode);
});

Something like

$builder->whereHas($relation, function (Builder $builder) use ($constraints, $mode) {
    foreach($constraints as $constraint){
        $this->doApply($builder, $constraint->field, $mode);
    }
});

Of course that's just hypothetical, there is no access to the group of the same constraints as they are isolated in buildConstraints (Jedrzej/Searchable/SearchableTrait.php#L120)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions