An Csv datasource for CakePHP 3.5,3.6,3.7
Install composer and run:
composer require giginc/cakephp3-driver-csv
Now, you need to set the connection in your config/app.php file:
'Datasources' => [
...
'csv' => [
'className' => 'Giginc\Csv\Database\Connection',
'driver' => 'Giginc\Csv\Database\Driver\Csv',
'baseDir' => './', // local path on the server relative to CONFIG
],
],
After that, you need to load Giginc\Csv\ORM\Table in your tables class:
//src/Model/Table/ProductsTable.php
namespace App\Model\Table;
use Giginc\Csv\ORM\Table;
class ProductsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setPrimaryKey('id');
$this->setSchemaRow(1); // Schema row is 1 row.
$this->setDelimiter(','); // default ,
$this->setEnclosure('"'); // default "
$this->setEscape("\\"); // default \\
$this->setTable('products'); // load file is CONFIG/materials.csv
}
/**
* Returns the database connection name to use by default.
*
* @return string
*/
public static function defaultConnectionName()
{
return 'csv';
}
/**
* findOk
*
* @param \League\Csv\Statement $query Query.
* @param array $options Option.
* @access public
* @return \Cake\ORM\Query
*/
public function findOk($query, array $options)
{
$query = $query
->where(function(array $row) {
return $row['status'] == 'ok';
});
return $query;
}
}
namespace App\Controller;
use App\Controller\AppController;
/**
* Pages Controller
*
* @property \App\Model\Table\PagesTable $Pages
*
* @method \App\Model\Entity\Review[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class PagesController extends AppController
{
/**
* Index method
*
* @access public
* @return \Cake\Http\Response|void
*/
public function index()
{
$this->loadModel('Products');
$data = $this->Products->find();
}
/**
* View method
*
* @param mixed $id
* @access public
* @return \Cake\Http\Response|void
*/
public function view($id)
{
$this->loadModel('Products');
$data = $this->Products->get(1);
}
}
Let's see a quick example:
//config/products.csv
id,category,name,price
1,"iphone","iPhone",8000
2,"macbook_pro","Macbook Pro",150000
3,"redmi_3s","Redmi 3S Prime",12000
4,"redmi_4x":"Redmi 4X",15000
5,"macbook_air":"Macbook Air",110000
6,"macbook_air":"Macbook Air 1",81000