PHP - 空合并运算符
PHP - null coalescing operator
在我的 Symfony 项目中,我写了一个方法,我需要在其中检查数据库中的表。
我正在寻找我在 API 调用中传递的 id 参数。它必须与定义为 $this->getUser().
的登录用户相同
我有 $account 和 $paymentDevice 其中 $account->getUser() 返回 null 因为该实体不存在在分贝。它存在于 $paymentDevice->getUser() 但它就像 || (OR) 没有得到第二个值。我尝试使用 ?: , ?? 但没有成功。
当我替换订单时它起作用了。
如何解决?
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!'); }
}
你也这样尝试过吗?
$account = $this->getAccountRepository()->find($id);
$paymentDevice= $this->getPaymentDeviceRepository()->find($id);
$service = $account ?? $paymentDevice;
if ($service !== null && $service->getUser() === $this->getUser()) {
//
}
在我的 Symfony 项目中,我写了一个方法,我需要在其中检查数据库中的表。
我正在寻找我在 API 调用中传递的 id 参数。它必须与定义为 $this->getUser().
的登录用户相同我有 $account 和 $paymentDevice 其中 $account->getUser() 返回 null 因为该实体不存在在分贝。它存在于 $paymentDevice->getUser() 但它就像 || (OR) 没有得到第二个值。我尝试使用 ?: , ?? 但没有成功。 当我替换订单时它起作用了。
如何解决?
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!'); }
}
你也这样尝试过吗?
$account = $this->getAccountRepository()->find($id);
$paymentDevice= $this->getPaymentDeviceRepository()->find($id);
$service = $account ?? $paymentDevice;
if ($service !== null && $service->getUser() === $this->getUser()) {
//
}