PHP - null coalescing operator

Jhonas

In my Symfony project I wrote a method where I need to check to tables in db.

I am looking for an id parameter that I passed in my API call. It has to be the same as for logged in user which is defined $this->getUser().

I have $account and $paymentDevice of which $account->getUser() is returning null as that entity does not exist in db. It exists in $paymentDevice->getUser() but it's like || (OR) does not get to the second value. I tried with ?: , ?? but non of the work. When I replace the order it works.

How to fix that?

public function clearSomething($id)
 {
        $type = $this->getTypeRepository()->findOneBy([
            'id' => $id
        ]);

    $account = $this->getAccountRepository()->find($id);
    $paymentDevice= $this->getPaymentDeviceRepository()->find($id);

    if ($account->getUser() === $this->getUser() || $paymentDevice->getUser() ===  $this->getUser()) {
        $this->em->remove($type);
        $this->em->flush();
    } else { throw new \Exception('Type does not belong to this Account/PaymentDevice!'); }

 }
Mihai Matei

Did you try it like this too?

$account = $this->getAccountRepository()->find($id);
$paymentDevice= $this->getPaymentDeviceRepository()->find($id);

$service = $account ?? $paymentDevice;
if ($service !== null && $service->getUser() === $this->getUser()) {
    //
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP Null coalescing operator usage

PHP ternary operator vs null coalescing operator

What is null coalescing assignment ??= operator in PHP 7.4

want to use PHP Null coalescing operator (??) with comperison

using PHP's null coalescing operator on an array

PHP null coalescing with ternary operator (again)

PHP - foreach lose reference with null coalescing operator

Why PHP isset and Null coalescing operator is throwing Notice with concatenation operator?

php null coalescing operator in combination with concatenation operator does not work right

Is there a "null coalescing" operator in JavaScript?

Null coalescing operator (??) with return

Null coalescing operator override

Is there a fancy and short solution for using the null coalescing operator with constants in PHP?

PHP null coalescing operator confusion - how to use in an if statement?

Null coalescing operator, unpredictable behavior

null-coalescing operator in the getter

Null coalescing operator not mitigating error

Usage of ?? operator (null-coalescing operator)

PHP - when to use error suppressing operator ( @( $obj->var ) ) and when to use null coalescing operator ( ?? )

sql null in c# and null coalescing operator

Does null coalescing operator call a function twice?

Null coalescing operator in React JS/ Typescript

Performance of expression behind null coalescing operator

Null-coalescing operator inside ternary

How to define a null-coalescing operator for Twig?

is there a Java equivalent to null coalescing operator (??) in C#?

Unique ways to use the Null Coalescing operator

Is there an equivalent of isset or a null coalescing operator in SASS?

Negated null coalescing operator (double question mark - ??)