I added csv export in admin grid and want to have export functionality whether admin user have filtered results or not. now I'm filtering data by daterange. some dates are working properly and gives me csv file like this and some dates not giving me a csv file and all I'm getting is a blank page.
all data export not working at all. this is my code to create a csv file:
$component = $this->filter->getComponent();
$this->filter->prepareComponent($component);
$dataProvider = $component->getContext()->getDataProvider();
$dataProvider->setLimit(0, false);
$searchResult = $component->getContext()->getDataProvider()->getSearchResult();
$name = md5(microtime());
$file = 'export/' . $component->getName() . $name . '.csv';
$this->directory->create('export');
$stream = $this->directory->openFile($file, 'w+');
$stream->lock();
$header = [
'Customer ID',
'Customer Name',
'Customer Mobile',
'Comment To Customer',
'Comment To Admin',
'Balance Change',
'Balance',
'Transaction Date',
'Type',
'Customer Notified',
'Created By',
];
$stream->writeCsv($header);
// var_dump(count($searchResult->getItems()));
foreach ($searchResult->getItems() as $document) {
$content = [
$this->customerRepositoryInterface->get($document->getCustomerEmail())->getId(),
$document->getCustomerName(),
$this->customerRepositoryInterface->get($document->getCustomerEmail())->getCustomAttribute('mobile')->getValue(),
$document->getCommentToCustomer(),
$document->getCommentToAdmin() ?? '',
number_format($document->getBalance(),0,',',','),
number_format($document->getCurrentBalance(),0,',',','),
$shamsi->toJalaliTime($document->getTransactionDate()),
(new TransactionType)->getBalanceUpdateLabel($document->getType()),
(new NotifiedStatus)->getStatusValue($document->getBalanceUpdateNotified()),
$document->getCreatedBy() ? $document->getCreatedBy() : '',
];
// echo "<pre>";
// var_dump($content);
// echo "</pre>";
$stream->writeCsv($content);
}
$stream->unlock();
$stream->close();
$date = $shamsi->toJalaliTime((new \DateTime())->format('Y-m-d H:i:s'));
return $this->fileFactory->create("TransactionReport_$date.csv", [
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
], 'var');
before foreach i wrote count($searchResult->getItems())
result is correct,
but after foreach i don't have any data! this is my var_dump() data after getting a blank page :
as you can see array (size=353)
353 records!