giginc/cakephp3-driver-csv

An CSV datasource for CakePHP 3.5

Installs: 277

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 5

Forks: 0

Open Issues: 0

Type:cakephp-plugin

v1.0.9 2020-09-02 01:18 UTC

This package is auto-updated.

Last update: 2024-04-29 04:27:12 UTC


README

An Csv datasource for CakePHP 3.5,3.6,3.7

Installing via composer

Install composer and run:

composer require giginc/cakephp3-driver-csv

Defining a connection

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
    ],
],

Models

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;
    }
}

Controllers

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

LICENSE

The MIT License (MIT) Copyright (c) 2020