vanqard / picotable
一个小型库,用于将对象连接到数据库行,以便快速原型设计
Requires
- php: >=5.5
Requires (Dev)
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-18 19:12:31 UTC
README
一个小型库,可以向任何对象添加存储。它的目标是帮助快速原型设计。
它并不打算取代一个正确指定和功能齐全的持久层,并且绝对不应在生产代码中使用。
场景
您需要快速搭建一个原型。引入您选择的微框架是很好的,但是添加数据库连接却是一件头疼的事情。
不再是了!只需将picotable添加到您的原型中,就可以快速而简单地给您的模型添加持久性。
基本原理
可以通过以下方式将持久性添加到对象中:
- 将提供的特质添加到对象的类中
- 向对象的类提供_columnMap数组属性,以将对象属性链接到表列
- 将实例化的对象传递给Connector->connect()方法,以将对象链接到数据库表
- 使用特质load()、save()和delete()方法执行CRUD-like操作
安装
只需使用composer将picotable添加到您的原型中
composer require vanqard/picotable
用法
您已经构建了一个示例模型类,并且需要将每个模型连接到数据库表行。
以下是通过以下步骤使其工作。
修改您的模型类
假设您已经有了以下POPO(Plain Old PHP Object):
class UserModel
{
private $name;
private $email;
private $age;
}
要将其连接到数据库表,在安装了picotable之后,您需要像这样编辑模型类:
use \Vanqard\Picotable\Traits\Storable as StorageTrait;
use \Vanqard\Picotable\Interfaces\StorableInterface;
class UserModel implements StorableInterface
{
/**
* Properties set to 'public' just for illustration. Feel free to
* make them private and provide getters and setters / mutators as required
*/
public $name;
public $email;
public $age;
// Add the following
private $usersId;
private $_columnMap = [
"usersId" => "users_id", // Maps local property to db column
"name",
"email",
"age"
];
use StorageTrait;
}
请注意我们添加了什么。
首先,我们添加了一个$usersId属性,以对应于表的键值。
其次,我们添加了一个$_columnMap属性,它声明了哪些对象属性应该持久化。请注意,此数组的第一项既提供了键又提供了值。在这种情况下,键是对象属性名称,值是相应的数据库表列名称。这是为了提供名称不同时的翻译。
此数组的其他元素(名称、电子邮件和年龄)不需要这种处理,因为名称在对象和数据库表中都是相同的。
无论如何,好消息是您现在可以持久化模型对象了。
嗯,几乎是这样。
您需要在某处设置连接。您可以通过Connector对象来完成此操作。通常,在处理任何请求之前,您会在启动过程中某处实例化Connector实例。
以下是执行此操作的代码。
$connector = new \Vanqard\Picotable\Connector($dsn, $user, $pass);
如您所见,您需要为连接器构造函数提供连接参数。以下是一些示例:
// SQLite
$dsn = "sqlite:prototype.db";
$user = $pass = null;
// MySQL
$dsn = "mysql:host=localhost;dbname=prototype";
$user = "root";
$pass = "secret";
好的,看起来不错。
那么,我们如何将模型对象连接到数据库表呢?
像这样
$myUser = new UserModel();
$connector->connect($myUser, 'users'); // Assume db table name is 'users'
就是这样?是的,没错。现在您可以随意处理模型对象了
保存新的模型
$myUser = new UserModel();
$connector->connect($myUser, 'users');
$myUser->name = "joe";
$myUser->email = "joe@example.com";
$myUser->age = 42;
$myUser->_save();
echo $myUser->usersId; // Just to confirm that we've saved successfully
从数据库加载模型
$myUser = new UserModel();
$connector->connect($myUser, 'users');
// Pass the primary key value to the _load method
$myUser->_load(1);
echo $myUser->name; // Outputs 'joe' if joe is the name identified by the primary key value "1"
使用模型更新数据库
$myUser = new UserModel();
$connector->connect($myUser, 'users');
$myUser->_load(1); // Loads the row for 'joe'
$myUser->name = "mary";
$myUser->email = "mary@example.com";
$myUser->age = "27";
$myUser->_save(); // Save knows when to insert and when to update
删除数据库中的行
$myUser = new UserModel();
$connector->connect($myUser, 'users');
$myUser->_load(1); // Loads the row for 'joe'
// ... sometime later
$myUser->_delete(); // Row is deleted for 'joe'
待办事项
- 错误处理 - 目前几乎没有任何处理
- 单元测试 - 这些也几乎不存在,但嘿,这是第一个提交。(并且在我的机器上它运行得很好)
安全问题
如果您在此代码中发现任何安全问题,请直接联系作者,邮箱为:thunder@vanqard.com