nextras / orm

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

Filter by relation 1:1 #525

Closed romanmatyus closed 3 years ago

romanmatyus commented 3 years ago

I have table BankTransaction with operations on bank account.

Next table is Order. Order is specific, is always payed only with max one transaction.

Transaction can be paired with Order, but not necessarily.

/**
 * @property BankTransaction|null  $bankTransaction {1:1 BankTransaction::$order, isMain=true}
 */
class Order extends Entity {}
/**
 * @property Order|null              $order {1:1 Order::$bankTransaction}
 */
class BankTransaction extends Entity {}

Is possible filter all BankTransation where is not paired to Order?

Something like this (inspired by Nette Database Explorer):

foreach ($this->orm->bankTransactions->findBy([
    ':order->bankTransaction' => null, // or 'order' => null
]) as $bankTransaction) {}

Thanks!

mabar commented 3 years ago

You don't have to care whether the relationship is stored on Order side, just filter by the property of entity from current side. Where is the relationship stored orm knows from isMain=true

$this->orm->bankTransactions->findBy([
    'order' => null,
])

Btw, did you know there is an IntelliJ plugin that can help you with that syntax? https://github.com/nextras/orm-intellij

hrach commented 3 years ago

Mabar's answer should be the answer. Thanks!