faslatam/orm
最新稳定版本:v1.0.0
Composer 安装命令:
composer require faslatam/orm
包简介
Tiny ORM for simple CRUD operations.
关键字:
README 文档
README
Small ORM for basic CRUD operations.
Install
Via Composer
$ composer require faslatam/orm
Usage
Model
Create a model
Any model extends the class \Forestry\Orm\BaseModel.
class User extends \Forestry\Orm\BaseModel { public static $database = 'example'; public static $table = 'users'; }
You have to define at least the database and table name.
Using the model
You can define getters and setter for all table fields.
$user = new User; $user->setName('Bob'); $user->save();
Getters/setters are not mandatory. You can access the properties directly:
$user = new User; $user->name = 'Bob'; $user->save();
Instead of calling the
save()method, you can explicitly callinsert()orupdate(). If you set the primary key on a new model object, you have to use the insert() method.
Connections
\Forestry\Orm\Storage provides a registry for PDO instances.
Set a connection
A connection is defined with the set() method:
\Forestry\Orm\Storage::set('default', [ 'dsn' => 'mysql:host=localhost', 'user' => 'root', 'password' => '', 'option' => [/* any PDO options can be defined here */] ]);
A model could use another connection if you configure it:
use \Forestry\Orm\Storage; use \Forestry\Orm\BaseModel; Storage::set('myOtherConnection', [ 'dsn' => 'mysql:host=127.0.0.1', 'user' => 'root', 'password' => '' ]); class Acme extends BaseModel { public static $storage = 'myOtherConnection'; public static $table = 'acme_table'; }
If you try to set an already defined connection
set()throws aLogicException.
Get a defined connection
You can also freely use the PDO connection like this:
Storage::get('myOtherConnection')->exec('SELECT * FROM example.users');
If you try to get an undefined connection
get()throws aOutOfBoundsException.
Close a connection
To close a defined connection use the delete() method:
Storage::delete('myOtherConnection');
If you try to close an undefined connection
delte()throws aOutOfBoundsException.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-09