2017 © Pedro Peláez
 

library mysql-csv

Export csv with mysql easier

image

juniorzavaleta/mysql-csv

Export csv with mysql easier

  • Sunday, June 11, 2017
  • by JuniorZavaleta
  • Repository
  • 1 Watchers
  • 0 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

MySql Exporter

Create a csv file from mysql, (*1)

Installation

composer require, (*2)

Mysql Configuration

Add the csv folder to file /etc/apparmor.d/usr.sbin.mysqld, (*3)

/usr/sbin/mysqld {
    [...]
    # Allow data files dir access
    /var/lib/mysql-files/ r,
    /var/lib/mysql-files/** rwk,

    /path/to/project/csv/ r,
    /path/to/project/csv/* rwk,
    [...]
}

then run, (*4)

sudo /etc/init.d/apparmor reload

Maybe have problems with secure-file-priv, change on mysql config, (*5)

[mysqld]
secure-file-priv = ""
sudo service mysql restart

Example

On Laravel or a framework with the function env() with the same behavior, (*6)

The second arg of the constructor are credentials for mysql, check source code, (*7)

$generator = new CsvGenerator('complaints');

$generator->setTitles(
    'Id',
    'Tipo contaminacion',
    'Distrito',
    'Estado',
    'Fecha de registro'
);

$generator->setColumns(
    'complaints.id',
    'contamination_types.description',
    'districts.name',
    'complaint_states.description',
    'complaints.created_at'
);

$generator->setFilename(storage_path('app/csv/casos_'.date('d_m_Y_H_i_s').'.csv'));

$generator->join('contamination_types', 'complaints.type_contamination_id', 'contamination_types.id')
    ->join('districts', 'complaints.district_id', 'districts.id')
    ->join('complaint_states', 'complaints.complaint_state_id', 'complaint_states.id');

if (!$user->is_admin) {
    $generator->where('complaints.district_id', session('district_id'))
              ->whereNot('complaint_state_id', Complaint::INCOMPLETED);
}

$generator->whereIf('complaint_states.description', request('estado'))
          ->whereIf('districts.name', request('distrito'))
          ->whereIf('contamination_types.description', request('tipo_contaminacion'))
          ->orderBy('complaints.id', 'DESC');

$filename = $generator->execute();

return response()->download($filename)->deleteFileAfterSend(true);

Export!, (*8)

The Versions

11/06 2017

dev-master

9999999-dev

Export csv with mysql easier

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

by Junior Zavaleta

11/06 2017

dev-dev

dev-dev

Export csv with mysql easier

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

by Junior Zavaleta

11/06 2017

v1.0

1.0.0.0

Export csv with mysql easier

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

by Junior Zavaleta