public function exportQuery(PDOStatement $stm, array $intersectKeys = null)
{
$all = $stm->fetchAll(PDO::FETCH_ASSOC);
if (empty($all)) {
return false;
}
$columns = array_keys($all[0]);
if ($intersectKeys) {
$columns = array_intersect_key($columns, array_flip($intersectKeys));
}
$php54 = version_compare(phpversion(), '5.5.0') < 0;
if ($php54) {
fputcsv($this->fd, $columns, $this->delimiter, $this->enclosure);
} else {
fputcsv($this->fd, $columns, $this->delimiter, $this->enclosure, $this->escapeChar);
}
foreach ($all as $row) {
$fields = [];
foreach ($keys as $key) {
$fields[] = $row[$key];
}
if ($php54) {
fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure);
} else {
fputcsv($this->fd, $fields, $this->delimiter, $this->enclosure, $this->escapeChar);
}
}
return true;
}