giginc/cakephp3-driver-csv

CakePHP 3.5 的 CSV 数据源

安装次数: 291

依赖项: 0

建议者: 0

安全: 0

星标: 2

关注者: 5

分支: 0

开放性问题: 0

类型:cakephp-plugin

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

This package is auto-updated.

Last update: 2024-09-29 05:48:45 UTC


README

为 CakePHP 3.5, 3.6, 3.7 提供的 Csv 数据源

通过 composer 安装

安装 composer 并运行

composer require giginc/cakephp3-driver-csv

定义连接

现在,您需要将连接设置在您的 config/app.php 文件中

 'Datasources' => [
...

    'csv' => [
        'className' => 'Giginc\Csv\Database\Connection',
        'driver' => 'Giginc\Csv\Database\Driver\Csv',
        'baseDir' => './', // local path on the server relative to CONFIG
    ],
],

模型

之后,您需要在您的 tables 类中加载 Giginc\Csv\ORM\Table

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

}

让我们看看一个简单的例子

//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

授权协议

MIT 许可证 (MIT) 版权所有 (c) 2020