fivesqrd/atlas-foundation
Composer 安装命令:
composer require fivesqrd/atlas-foundation
包简介
Simple Data Mapper Library
README 文档
README
Atlas is an open source data mapper implementation for PHP.
Atlas creates barebones models for your project with minimal effort, allowing you to start working with them quickly. Extending or customising functionality is possible, but can wait until it is required.
The framework offers the following features:
- Minimal scaffolding required to create new models
- Easily expose business logic query layer
- Reduced application wide ripples from schema changes
- Automatic read/write routing
- Protection against SQL injection attacks
- RDBMS abstraction
Use Cases
Persisting a new user:
$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);
Fetching an instance of the user entity by primary key:
$user = $this->model(Model\User::class)->fetch($id);
Access properties using default getters:
$timestamp = $user->get('_lastLogin');
Persisting changes to the user model using default setters:
/* 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);
Querying user model business layer:
$users = $this->model(Model\User::class)->query()
->isEnabled(true)
->hasLoggedSince(strtotime('5 days ago'))
->fetch()->all();
foreach ($users as $user) {
echo $user->get('_email');
}
Optimised queries for simple operations like counts:
$count = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->count();
Extending Model Classes
Access properties using custom getters:
$date = $user->getLastLogin('Y-m-d');
Persisting changes using custom setters:
/* 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);
Using named queries for consistent results:
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->all();
Adding to named queries on the fly:
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
->isEnabled(true)
-fetch()->all();
Performing operations on collections:
$users = $this->model(Model\User::class)->named()
->withRecentLogIn()
-fetch()->all();
$emails = $users->getAllEmailAddresses();
Implementation
Each model consists of a set of classes. Each class extends a super class, to allow new models to be created with minimal effort.
Below is an example what a project with 3 models might look like
|- Model
|-- User.php
|-- User
|-- Entity.php
|-- Mapper.php
|-- Collection.php
|-- Query.php
|-- Named.php
|-- Relation.php
|-- Customer.php
|-- Customer
...
|-- Content.php
|-- Contact
...
Sample mapper classs
<?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');
}
Sample entity class
<?php
namespace Application\Model\User;
class Entity extends \Atlas\Model\Entity
{
protected $_id;
protected $_email;
protected $_password;
protected $_lastLogin;
}
Using Canvas
The atlas repo ships with a script to quickly create boilerplate classes when a new model needs to be added. See https://github.com/fivesqrd/atlas-canvas
Install and Setup
Install
Via composer
cd /myproject
php composer.phar require fivesqrd/atlas:3.0
Config
Add the following config to your project:
$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',
),
);
Bootstrap from MVC
Atlas can be bootstrapped from within your MVC framework by passing the Proxy class to your controllers/views via a plugin or helper:
class MyControllerPlugin
{
public function model($class) {
return new Atlas\Proxy(
new Atlas\Database\Factory($this->_config),
new Atlas\Database\Resolver($class)
);
}
}
A Laravel 5 specific package is available here: https://github.com/fivesqrd/atlas-laravel
fivesqrd/atlas-foundation 适用场景与选型建议
fivesqrd/atlas-foundation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.23k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 fivesqrd/atlas-foundation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fivesqrd/atlas-foundation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-11-09