LazyRecord\Exporter\CSVExporter::exportCollection PHP Метод

exportCollection() публичный Метод

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )
public exportCollection ( BaseCollection $collection, array $intersectKeys = null )
$collection LazyRecord\BaseCollection
$intersectKeys array
    public function exportCollection(BaseCollection $collection, array $intersectKeys = null)
    {
        $schema = $collection->getSchema();
        $keys = $schema->getColumnNames();
        if ($intersectKeys) {
            $keys = array_intersect_key($keys, array_flip($intersectKeys));
        }
        $php54 = version_compare(phpversion(), '5.5.0') < 0;
        if ($php54) {
            fputcsv($this->fd, $keys, $this->delimiter, $this->enclosure);
        } else {
            fputcsv($this->fd, $keys, $this->delimiter, $this->enclosure, $this->escapeChar);
        }
        foreach ($collection as $record) {
            $array = $record->toArray();
            $fields = [];
            foreach ($keys as $key) {
                $fields[] = $array[$key];
            }
            if ($php54) {
                fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure);
            } else {
                fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure, $this->escapeChar);
            }
        }
        return true;
    }

Usage Example

Пример #1
0
 /**
  * Export collection to PHP output stream.
  *
  * @param BaseCollection $collection
  */
 public function exportOutput(BaseCollection $collection, $attachmentName = null)
 {
     $filename = $attachmentName ?: $this->defaultFilename();
     // use "text/csv" according to RFC 4180.
     header("Content-Type: text/csv; charset=UTF-8");
     header("Content-Disposition: attachment; filename={$filename}");
     $outputFd = fopen('php://output', 'w');
     $exporter = new BaseCSVExporter($outputFd);
     $exporter->exportCollection($collection, $this->exportFields);
 }
All Usage Examples Of LazyRecord\Exporter\CSVExporter::exportCollection