nextras / orm

Orm with clean object design, smart relationship loading and powerful collections.
https://nextras.org/orm
MIT License
310 stars 59 forks source link

Condition over table (without `->id`) is not working #574

Open hrach opened 2 years ago

hrach commented 2 years ago

To Reproduce

/**
 * @property-read string         $id        {primary}
 * @property AlarmDeparture|null $departure {1:1 AlarmDeparture::$alarm}
 */
final class Alarm extends Entity
{

}

/**
 * @property-read string $id    {primary}
 * @property-read Alarm  $alarm {1:1 Alarm::$departure, isMain=true}
 */
final class AlarmDeparture extends Entity
{

}

$alarmRepository->findBy([
    'departure!=' => null,
])->fetchAll();

Produces

Nextras\Dbal\Drivers\Exception\QueryException: ERROR:  column alarms . departure does not exist
LINE 1: ...'alarms' .* FROM 'app' . 'alarms' as 'alarms' WHERE('alarms' . "...

Expected behavior It works.

Workaround


$alarmRepository->findBy([
    'departure->id!=' => null,
])->fetchAll();
hrach commented 1 month ago

Current status:

It is not working on the non-main side. The main side works due to the implementation detail that the property is mapped to the actual column (alarm_id) and then the comparison works.

Since we do not do any optimization when alarm->id would be optimized to alarm_id comparison without a join, I am not super sure that what the future is here - if we want to support the shortcut as it is; expand its behavior to auto-optimize and/or support it on the non-main side.

Also, the Orm IntelliJ plugin auto-adds -> after picking "departure".

hrach commented 1 month ago

I am marking it as a feature request.

hrach commented 1 month ago

testcase:


    public function testFilterAutomaticallyById(): void
    {
        $ean = new Ean();
        $ean->code = '1234';
        $ean->book = $this->orm->books->getByIdChecked(1);
        $this->orm->eans->persistAndFlush($ean);
        $eanId = $ean->id;
        $this->orm->clear();

        $ean = $this->orm->books->findBy(['ean' => $eanId])->fetch();
        Assert::notNull($ean);

        $book = $this->orm->eans->findBy(['book' => 1])->fetch(); // failure
        Assert::notNull($book);
    }