orm icon indicating copy to clipboard operation
orm copied to clipboard

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

Open hrach opened this issue 3 years ago • 3 comments

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 avatar Apr 20 '22 18:04 hrach

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 avatar Oct 11 '24 22:10 hrach

I am marking it as a feature request.

hrach avatar Oct 11 '24 22:10 hrach

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);
	}

hrach avatar Oct 12 '24 17:10 hrach