forestry / orm
该包已废弃,不再维护。未建议替代包。
简单的CRUD操作ORM。
0.1.0
2015-03-27 12:45 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2019-11-04 10:13:55 UTC
README
用于基本CRUD操作的小型ORM。
安装
通过Composer
$ composer require forestry/orm
使用方法
模型
创建模型
任何模型都继承自类\Forestry\Orm\BaseModel
。
class User extends \Forestry\Orm\BaseModel { public static $database = 'example'; public static $table = 'users'; }
您必须定义数据库和表名。
使用模型
您可以定义所有表字段的getter和setter。
$user = new User(); $user->setName('Bob'); $user->save();
getter/setter不是必需的。您可以直接访问属性
$user = new User(); $user->name = 'Bob'; $user->save();
您可以直接调用
insert()
或update()
而不是调用save()
方法。如果在新模型对象上设置了主键,您必须使用insert()方法。
连接
\Forestry\Orm\Storage
提供了一个PDO实例注册表。
设置连接
使用set()
方法定义连接
\Forestry\Orm\Storage::set('default', [ 'dsn' => 'mysql:host=localhost', 'user' => 'root', 'password' => '', 'option' => [/*any PDO options can be defined here*/] ]);
如果进行配置,模型可以使用另一个连接
use \Forestry\Orm\Storage as Storage; use \Forestry\Orm\BaseModel as 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'; }
如果您尝试设置已定义的连接
set()
会抛出LogicException
。
获取已定义的连接
您也可以像这样自由使用PDO连接
Storage::get('myOtherConnection')->exec('SELECT * FROM example.users');
如果您尝试获取未定义的连接
get()
会抛出OutOfBoundsException
。
关闭连接
要关闭已定义的连接,请使用delete()
方法
Storage::delete('myOtherConnection');
如果您尝试关闭未定义的连接
delte()
会抛出OutOfBoundsException
。
测试
$ phpunit
贡献
有关详细信息,请参阅CONTRIBUTING
致谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件