giginc/cakephp3-driver-json

CakePHP 3.0 的 JSON 数据源

安装次数: 3,271

依赖者: 0

建议者: 0

安全: 0

星星: 1

关注者: 5

分支: 0

公开问题: 0

类型:cakephp-plugin

1.0.0 2019-08-26 05:47 UTC

This package is auto-updated.

Last update: 2024-09-25 16:09:14 UTC


README

CakePHP 3.5 的 JSON 数据源

通过 composer 安装

安装composer并运行

composer require giginc/cakephp3-driver-json

定义连接

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

 'Datasources' => [
    'default' => [
        'className' => 'Giginc\Json\Database\Connection',
        'driver' => 'Giginc\Json\Database\Driver\Json',
        'baseDir' => './', // local path on the server relative to WWW_ROOT
    ],
],

模型

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

//src/Model/Table/YourTable.php

use Giginc\Json\ORM\Table;

class CategoriesTable extends Table {

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('categories'); // load file is WWW_ROOT/categories.json
    }

}

控制器

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
     *
     * @return \Cake\Http\Response|void
     */
    public function index()
    {
        $this->loadModel('TestJsons');
        $data = $this->TestJsons->find()
            ->where('code', '=', "0001")
            ->get()
            ;
    }
}

您可以使用各种查询方法如 findwhereorWherewhereInwhereStartsWithwhereEndsWithwhereContains 等来查询数据。同时,您可以在查询后使用 sumcountgroupBymaxmin 等来聚合数据。

让我们看一个快速示例

//data.json
{
	"name": "products",
	"description": "Features product list",
	"vendor":{
		"name": "Computer Source BD",
		"email": "info@example.com",
		"website":"www.example.com"
	},
	"users":[
		{"id":1, "name":"Johura Akter Sumi", "location": "Barisal"},
		{"id":2, "name":"Mehedi Hasan Nahid", "location": "Barisal"},
		{"id":3, "name":"Ariful Islam", "location": "Barisal"},
		{"id":4, "name":"Suhel Ahmed", "location": "Sylhet"},
		{"id":5, "name":"Firoz Serniabat", "location": "Gournodi"},
		{"id":6, "name":"Musa Jewel", "location": "Barisal", "visits": [
			{"name": "Sylhet", "year": 2011},
			{"name": "Cox's Bazar", "year": 2012},
			{"name": "Bandarbar", "year": 2014}
		]}
	],
	"products": [
		{"id":1, "user_id": 2, "city": "bsl", "name":"iPhone", "cat":1, "price": 80000},
		{"id":2, "user_id": 2, "city": null, "name":"macbook pro", "cat": 2, "price": 150000},
		{"id":3, "user_id": 2, "city": "dhk", "name":"Redmi 3S Prime", "cat": 1, "price": 12000},
		{"id":4, "user_id": 1, "city": null, "name":"Redmi 4X", "cat":1, "price": 15000},
		{"id":5, "user_id": 1, "city": "bsl", "name":"macbook air", "cat": 2, "price": 110000},
		{"id":6, "user_id": 2, "city": null, "name":"macbook air 1", "cat": 2, "price": 81000}
	]
}
$this->loadModel('TestJsons');
$data = $this->TestJsons->find('products')
    ->where('cat', '=', 2)
    ->get();
dd($res);

//This will print
/*
array:3 [▼
  1 => {#7 ▼
    +"id": 2
    +"user_id": 2
    +"city": null
    +"name": "macbook pro"
    +"cat": 2
    +"price": 150000
  }
  4 => {#8 ▼
    +"id": 5
    +"user_id": 1
    +"city": "bsl"
    +"name": "macbook air"
    +"cat": 2
    +"price": 110000
  }
  5 => {#9 ▼
    +"id": 6
    +"user_id": 2
    +"city": null
    +"name": "macbook air 1"
    +"cat": 2
    +"price": 81000
  }
]
*/

特别感谢

php-jsonq

许可证

MIT 许可证 (MIT) 版权 (c) 2013