stevecomrie/baserow-php

Baserow.io API 的 PHP 客户端

0.0.7 2023-04-27 04:45 UTC

This package is auto-updated.

Last update: 2024-09-27 08:04:55 UTC


README

Baserow.io API 的 PHP 客户端,具有可读性强的表和字段名称。

欢迎评论、请求或错误报告。

入门指南

此库仅使用 Baserow API 管理CRUD操作。

它可以与SaaS托管版本和自托管版本一起使用。

有关Baserow API的更多信息,请参阅

安装

如果您使用Composer,可以运行以下命令

composer require "stevecomrie/baserow-php:main-dev"

您也可以直接下载src文件并将其提取到您的Web目录中。

将客户端添加到您的项目

如果您使用Composer,请运行自动加载器

require 'vendor/autoload.php';

或包含Baserow.php文件

include('../src/Baserow.php');
include('../src/Request.php');
include('../src/Response.php');

初始化类

只需要 api_key 参数,其余的作为引用添加。

您可以通过Baserow.io仪表板创建您的API密钥。

use \Scomrie\Baserow\Baserow;

$baserow = new Baserow([
    'api_key' => 'API_KEY', // REQUIRED!!

    // if you are self hosting your own instance of Baserow, use this parameter to
    // point to the proper location on your server. defaults to the SaaS hosted endpoint.
    'api_url' => 'https://api.baserow.io/api/database/rows/table'

    // if set to true, will dump any errors with print_r() and exit on failure
    'debug' => false,

    // map of all tables & fields from auto-generated ###'s to human readable names
    'table_map' => []
]);

创建可读性强的表映射

Baserow在创建新表和字段时使用递增整数作为名称。

例如,表分配ID如下

  • 10001
  • 10002
  • 10003,等等

表内的字段被赋予名称,例如

  • field_1001
  • field_1002
  • field_1003,等等

这显然不适合长期代码的可读性和维护,但考虑到Baserow目前的发展阶段,这是完全可以理解的。

为了使开发者的生活更容易一些,您可以在创建Baserow客户端新实例时配置 table_map 参数,这可以让您将这些自动生成的ID映射到可读性强的名称。

示例表映射

这是一个示例表映射配置可能的样子。

$table_map = [
	'Customers'  => [ 10001, [
		'firstName' =>  'field_1001',
		'lastName'  =>  'field_1002',
		'active'    =>  'field_1003',
		'projects'  =>  'field_1004',
	]],

	'Projects' => [ 10002, [
		'name'     => 'field_1005',
		'dueDate'  => 'field_1006',
		'customer' => 'field_1007',
	]],
];

您可以使用数据库的API文档找到Baserow生成的表和字段ID。

当您用 table_map 初始化Baserow客户端时,您可以在任何之前使用表####或field_####的地方使用您的新可读性强的名称。

table_map 参数是可选的,但本文件的其余示例将假设您正在使用它。

表映射建议

您可能不想在可读性强的字段名称中使用空格。

如果您添加或删除字段,则必须手动更新您的表映射配置。

除了在编写代码时保持您的理智之外,表映射配置还使您能够使用两个单独的数据库创建预发布/生产环境,每个数据库都有自己的表映射。

与API通信

从表中检索“页面”记录

获取“联系人”表中的条目。

$response = $baserow->list( 'Contacts' );

print_r($response);

使用 $params 过滤和排序记录

您不需要使用所有参数,它们被添加为引用。

$params = array(
    "page" => 1,
    "size" => 100,
    "order_by" => "lastName",
    "filter__firstName__equal" => "Steve",
    "include" => "firstName,lastName",
);

$response = $baserow->list( 'Contacts', $params );
print_r($response);

有关与 list()all() 函数一起使用的可用参数的完整列表,请参阅Baserow为您数据库自动生成的API文档

检索表中的所有记录

如果您需要返回多页结果,可以使用 all() 函数自动检索整个分页结果集。

$response = $baserow->all( 'Contacts', $params );
print_r($response);

创建新记录

我们将在“联系人”表中创建新条目

$newContact = [
	'firstName' => "Steve",
	'lastName'  => "Comrie",
	'active'    => true,
	'projects'  => [ 2 ],
];

if( $contact = $baserow->create( "Contacts", $newContact ) ) {
	echo "Created new contact: " . $contact->id . "\n";
} else {
	print_r( $baserow->error() );
}

更新现有记录

使用行ID来更新条目

$contactID = 1;
$updatedFields = [
	'firstName' => "Steven",
	'active'    => false,
];

if( $baserow->update( "Contacts", $updatedFields, $contactID ) ) {
	echo "Contact updated!\n";
}

删除记录

使用行ID来删除条目

$deleteContactID = 4;
if( $baserow->delete("Contacts", $deleteContactID ) ) {
	echo "Contact deleted!\n";
}

访问API错误信息

每当API操作失败时,您可以使用error()函数访问失败的原因。

$error = $baserow->error();
print_r( $error );

积分

版权所有 (c) 2021 - 由Steve Comrie编写。

感谢Bram Wiepjes开发了Baserow.io,并感谢Sleiman Tanios,此库大量借鉴了他的Airtable API客户端。