giginc / cakephp3-driver-cloud-firestore
CakePHP 3.5 的 Cloud Firestore 驱动器
0.0.2
2021-08-13 06:13 UTC
Requires
- php: ^5.6|^7.0
- cakephp/cakephp: ^3.5
- google/cloud-firestore: ^1.19
Requires (Dev)
- cakephp/cakephp-codesniffer: ^4.0
- phpunit/phpunit: ^6.0
- squizlabs/php_codesniffer: ^3.5
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', ]); } }