Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage innerJoin not nullable associations #22

Closed
soullivaneuh opened this issue Jan 24, 2018 · 5 comments
Closed

Manage innerJoin not nullable associations #22

soullivaneuh opened this issue Jan 24, 2018 · 5 comments

Comments

@soullivaneuh
Copy link

soullivaneuh commented Jan 24, 2018

With this PHP code:

$qb = $this->invoiceRepository->createQueryBuilder('i')
    ->select('i, c, ba')
    ->innerJoin('i.customer', 'c', Expr\Join::WITH, 'c.automaticPaymentMethod = :automatic_payment_method')
    ->innerJoin('c.bankAccount', 'ba', Expr\Join::WITH, 'ba.activated = :activated AND ba.mandateSigned = :mandateSigned AND ba.mandateSignedDate IS NOT NULL')

foreach ($invoices as $invoice) {
    $customer = $invoice->getCustomer();
    $bankAccount = $customer->getBankAccount();

    $bankAccount->setFirstDebit(true);
    $this->persist($bankAccount);
}
;

I'll have this error:

Parameter #1 $object of method Doctrine\Common\Persistence\ObjectManagerDecorator::persist() expects object, AppBundle\Entity\BankAccount|null given.

But this is not true as innerJoin will drop customers without a bank account. Am I right?

If I'm right, checking if $bankAccount in this case is overkill and will never match.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 24, 2018 via email

@soullivaneuh
Copy link
Author

This would be really hard to track in a codebase.

I don't know, but it's still a false positive AFAIK. IMHO, the issue should be still open. Maybe somebody will find a solution for that. 👍

you can fetch
customer with a bank account and later set it to null, confusing the
analyser.

I'm not sure to follow you here. What do you mean by set it to null? On the codebase? In this case, it can be done for anything. 🤔

Or are you referencing the fact that the return value of getBankAccount can be null? You are maybe right indeed, but what should we do in this case? A special method getExistingBankAccount without possible null return? Something else? 🤔

@ondrejmirtes
Copy link
Member

It's simple - getBankAccount() can always return null because you cannot be sure about the inner state of the object.

Yep, your suggestion is correct - you should use a non-nullable getter in cases you're sure there is definitely the bank account set.

@soullivaneuh
Copy link
Author

Ok I see, thanks for your time @ondrejmirtes! 👍

@github-actions
Copy link

github-actions bot commented May 2, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants