giginc/cakephp3-driver-cloud-firestore

CakePHP 3.5 的 Cloud Firestore 驱动器

0.0.2 2021-08-13 06:13 UTC

This package is auto-updated.

Last update: 2024-09-13 13:00:44 UTC


README

为 CakePHP 3.5, 3.6, 3.7 提供云 Firestore

通过 composer 安装

安装 composer 并运行

composer require giginc/cakephp3-driver-cloud-firestore

定义连接

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

 'Datasources' => [
...
    'cloud-firestore' => [
        'className' => 'Giginc\CloudFirestore\Database\Connection',
        'driver' => 'Giginc\CloudFirestore\Database\Driver\CloudFirestore',
        'projectId' => env('CLOUD_FIRESTORE_PROJECT_ID', 'project_id'),
        'keyFile' => [], // Console. Ex: json_decode(file_get_contents($path), true).
        'keyFilePath' => null, //The full path to your service account credentials .json file retrieved.
        'retries' => 3,
    ],

],

模型

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

//src/Model/Table/ProductsTable.php
namespace App\Model\Table;

use Giginc\CloudFirestore\ORM\Table;

/**
 * ProductsTable Table
 *
 * @uses Table
 * @package Table
 */
class ProductsTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('products');
    }

    public static function defaultConnectionName()
    {
        return 'cloud-firestore';
    }
}

实体

//src/Model/Entity/Product.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * Product Entity
 *
 * @uses Entity
 * @package Entity
 */
class Product extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
    ];

    protected $_virtual = [
    ];
}

## Controllers

```php
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');

        // select
        $data = $this->Products->find()
            ->document('1')
            ->first();

        // insert
        $this->Products->document('1')
            ->insert([
                'name' => 'iPhoneXR',
                'description' => 'iPhoneXR',
                'created_at' => '2021-04-21',
            ]);
    }
}

许可协议

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