Call to a member function toArray() on null
- neto737
- Member | 1
Hi,
I just started to use Nette Database on my project and I figured out that I'm having some trouble to handle some things…
I'm trying to do a SELECT from my database, but the point is, after that SELECT I have to format some values that comes from that request.
I figured out that I'm only able to do this if the request returns as an array (because I'm formatting the values using the same variable, like: $thing[‘amount’] = number_format($thing[‘amount’])).
The point is, if for some reason the request don't return anything I receive a exception, shouldn't toArray function return an empty array or simply null when there's nothing returned from database request?
Here's an example of the way I'm doing this:
public function getInvoiceDetails(string $data) {
return $this->explorer->table('invoices')->whereOr([
'uid' => $data,
'address' => $data
])->select('id, uid, email, ad_id, surf_id, type, address, cpc, amount, status, confirmations, timestamp')
->fetch()->toArray();
}
Is there any workaround? I read the docs and the API but it didn't help me at all…
- stepos2
- Member | 53
Not really a task for the framework. This is just about how you handle it with PHP.
public function getInvoiceDetails(string $data)
{
$row = $this->explorer->table('invoices')->whereOr([
'uid' => $data,
'address' => $data
])
->select('id, uid, email, ad_id, surf_id, type, address, cpc, amount, status, confirmations, timestamp')
->fetch();
return $row ? $row->toArray() : [];
}