ofmadsen / livid
PHP 数据映射器(ORM)
0.2.1
2017-12-30 10:54 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: ^5.2
This package is not auto-updated.
Last update: 2024-09-19 11:43:37 UTC
README
Livid 是一个 PHP 数据映射器(ORM)。
安装
使用以下命令安装最新版本
$ composer require ofmadsen/livid
特性
- 使用
PDO作为数据库驱动程序(支持多种数据库类型) - 支持连接到多个数据库(相同或不同类型)
- 延迟加载数据库连接(在查询时连接)
- 使用单个、所有或标量值查询结果
- 自动将列蛇形命名转换为实体属性驼峰命名
基本用法
引导
<?php $config = new Livid\Config\SQLite3Config(); $database = new Livid\Database($config); Livid\Mapper::setDatabase($database);
映射器
<?php class ExampleMapper extends Livid\Mapper { public function get($id) { $sql = 'SELECT * FROM examples WHERE id = :id'; $parameters = ['id' => $id]; return $this->query($sql, $parameters)->get() } } // Assuming the examples table contain a row with id = 1 $mapper = new ExampleMapper(); $entity = $mapper->get(1); // Will echo Livid\Entity as no entity object was specified in the Query::get method echo get_class($entity);
请注意,Livid\Mapper 没有定义构造方法,因此映射器可以自由地注入任何内容。将映射器注入到其他类中相对便宜,因为数据库连接在需要时才建立,并且跨映射器重用。
在定义的实体中获取数据
Livid 支持具有公共、受保护和私有属性的实体。Livid 中实体属性的命名约定为驼峰式,因此任何包含下划线的列名都会被转换。
示例实体
<?php class Example { private $id; private $exampleText; public function getExampleText() { return $this->exampleText; } }
示例映射器
<?php class ExampleMapper extends Livid\Mapper { public function get($id) { // Note that the database column name is in snake case form $sql = 'SELECT id, example_text FROM examples WHERE id = :id'; $parameters = ['id' => $id]; // Note that the get method on the query object is called with a reference to the Example class return $this->query($sql, $parameters)->get(Example::class) } } $mapper = new ExampleMapper(); $example = $mapper->get(1); echo $example->getExampleText();
由于实体是简单的容器对象,它们可以包含满足应用程序需求的方法和逻辑。
使用多个数据库
Livid 支持连接到多个数据库。未指定名称的数据库将使用默认名称。使用非默认数据库的映射器必须定义包含数据库名称的常量 DATABASE。
<?php class ExampleMapper extends Livid\Mapper { // Uses the default database - in this case the MySQL at 127.0.0.1 with database 'livid' - see below } class ExampleLoggerMapper extends Livid\Mapper { const DATABASE = 'logger'; }
如果多个映射器使用非默认数据库,如果它们扩展一个定义数据库名称的通用类,则可能更容易维护。
<?php // Default setup $config = new Livid\Config\MySQLConfig(); $config->setHost('127.0.0.1'); $config->setDatabase('livid'); $defaultDatabase = new Livid\Database($config); Livid\Mapper::setDatabase($defaultDatabase); // Logger setup $config = new Livid\Config\SQLite3Config(); $loggerDatabase = new Livid\Database($config); Livid\Mapper::setDatabase($loggerDatabase, ExampleLoggerMapper::DATABASE);
支持的数据库
目前,此库仅提供 PDO 支持的数据库子集。如果您的首选数据库不在以下列表中,请随时为此项目做出贡献。
MySQL
对于 MySQL 数据库,请使用 Livid\Config\MySQLConfig。
SQLite3
对于 SQLite3 数据库,请使用 Livid\Config\SQLite3Config。
接口
Livid\Mapper 可用的方法
<?php /** * Query the database. * * @param string $sql * @param mixed[] $parameters * * @return Livid\Query */ public function query($sql, array $parameters = []); /** * Execute a sql query on the database. * * @param string $sql * * @return int */ public function execute($sql);
Livid\Query 可用的方法
<?php /** * Get a single entity. * * If the result contains more than one entity an exception is thrown. * * @param string $entity * * @throws InvalidFetchedEntityCount * * @return mixed */ public function get($entity = Entity::class); /** * Get multiple entities. * * @param string $entity * * @return mixed[] */ public function all($entity = Entity::class); /** * Get a scalar value. * * @return mixed */ public function scalar(); /** * Bind parameters and execute the statement. * * @throws DatabaseQueryFailed * * @return bool */ public function execute();
贡献
所有人均可向 Livid 贡献。在实施大型功能或重大重构之前,请先取得联系。不用说,必须遵循编码风格,并且需要完整的测试覆盖率。
许可
Livid 在 MIT 许可证下提供 - 有关详细信息,请参阅 LICENSE 文件。