五次方平方根 / Atlas 基金会
此软件包最新版本(v3.2.15)没有可用的许可信息。
简单数据映射库
v3.2.15
2019-11-11 18:35 UTC
README
Atlas 是一个开源的 PHP 数据映射实现。
Atlas 可以用最小的努力为您的项目创建基本的模型,让您快速开始使用它们。扩展或自定义功能是可能的,但可以等到需要时再进行。
该框架提供以下功能
- 创建新模型所需的脚手架最小化
- 轻松暴露业务逻辑查询层
- 减少由模式更改引起的全局影响
- 自动读写路由
- 防止 SQL 注入攻击
- RDBMS 抽象
用例
持久化新用户
$user = new Model\User\Entity();
$user->set('_email', 'user@domain.com');
$user->set('_enabled', true);
/* Save to db */
$id = $this->model(Model\User::class)->save($user);
通过主键获取用户实体实例
$user = $this->model(Model\User::class)->fetch($id);
使用默认获取器访问属性
$timestamp = $user->get('_lastLogin');
使用默认设置器持久化用户模型更改
/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);
/* Update entity */
$user->set('_lastLogin', time());
/* Save to db */
$this->model(Model\User::class)->save($user);
查询用户模型业务层
$users = $this->model(Model\User::class)->query()
->isEnabled(true)
->hasLoggedSince(strtotime('5 days ago'))
->fetch()->all();
foreach ($users as $user) {
echo $user->get('_email');
}
优化简单操作(如计数)的查询
$count = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->count();
扩展模型类
使用自定义获取器访问属性
$date = $user->getLastLogin('Y-m-d');
使用自定义设置器持久化更改
/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);
/* Update entity */
$user->setEmailAddress('user@domain.com');
$user->setEnabled(true);
/* Save to db */
$this->model(Model\User::class)->save($user);
使用命名查询以获得一致的结果
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->all();
动态添加到命名查询中
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
->isEnabled(true)
-fetch()->all();
对集合执行操作
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->all();
$emails = $users->getAllEmailAddresses();
实现
每个模型由一组类组成。每个类扩展一个超类,以允许以最小的努力创建新模型。
以下是一个包含 3 个模型的项目示例
|- Model
|-- User.php
|-- User
|-- Entity.php
|-- Mapper.php
|-- Collection.php
|-- Query.php
|-- Named.php
|-- Relation.php
|-- Customer.php
|-- Customer
...
|-- Content.php
|-- Contact
...
示例映射类
<?php
namespace Application\Model\User;
class Mapper extends \Atlas\Model\Mapper
{
protected $_alias = 'u';
protected $_table = 'users';
protected $_key = 'id';
protected $_map = array(
'_id' => 'id',
'_email' => 'email',
'_password => 'password',
'_lastLogin' => 'last_login'
);
protected $_readOnly = array('id');
}
示例实体类
<?php
namespace Application\Model\User;
class Entity extends \Atlas\Model\Entity
{
protected $_id;
protected $_email;
protected $_password;
protected $_lastLogin;
}
使用画布
atlas 仓库附带一个脚本来快速创建在需要添加新模型时使用的样板类。请参阅 https://github.com/fivesqrd/atlas-canvas
安装和设置
安装
通过 composer
cd /myproject
php composer.phar require fivesqrd/atlas:3.0
配置
将以下配置添加到您的项目中
$config = array(
'read' => array(
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
'username' => 'username',
'password' => 'password',
),
'write' => array(
'dsn' => 'mysql:dbname=testdb;host=127.0.0.1',
'username' => 'username',
'password' => 'password',
),
);
从 MVC 引导
Atlas 可以通过将代理类传递给控制器/视图(通过插件或助手)来从 MVC 框架中引导。
class MyControllerPlugin
{
public function model($class) {
return new Atlas\Proxy(
new Atlas\Database\Factory($this->_config),
new Atlas\Database\Resolver($class)
);
}
}
这里有一个针对 Laravel 5 的特定软件包: https://github.com/fivesqrd/atlas-laravel