Possible bug in Agile Data 1.4.1 addCondition with OR?

#1

I have an application that has a tiered structure: a case study can have many scenarios. The user can define Events which can reference other Events (an event can also be on a specific date, or something else, but that’s not relevant for this example). If the user is defining an Event for a scenario, he should be able to reference any event from the case study or that specific scenario, but not other scenarios.

The database structure is a many to one relationship between Events and a Scenario. If an Event is related to the high-level CaseStudy then it has a scenario_id of null; if it’s related to a specific Scenario then it has a scenario_id of the id of that Scenario.

In my Event model init routine I have the following code snippet:

$this->hasOne('event_id', function ($m)
{
    $events = $m->ref('casestudy_id')->ref('events');
    $scenario = $m->ref('scenario_id')['id'];
    $events->addCondition([
        ['scenario_id', null],
        ['scenario_id', $scenario]];
    return ($events);
});

This works just fine. However, if I replace the addCondition with the shorter form:

$events->addCondition(‘scenario_id’, [null, $scenario]);

then it doesn’t work. The shorter version filters out all of the Events associated with the top-level CaseStudy (the ones with scenario_id of null). The order of the null and $scenario in the addCondition doesnn’t matter – both give the same results.

This behavior is showing up with atk4/data version 1.4.1. Is this a bug, or am I missing something?

#2

This does seem like a bug. Here is another way to approach it:

$event->addCondition($event->expr(
   '[scenario_id] is null or [scenario_id] = []', 
   [$scenario]
));