jetfirephp / db
JetFire - 数据库抽象层
dev-master
2017-03-26 19:28 UTC
Requires
- php: >=5.4.0
Suggests
- doctrine/orm: For Doctrine usage
- gabordemooij/redbean: For RedBean usage
This package is not auto-updated.
Last update: 2024-09-20 22:43:16 UTC
README
ORM 的独特外观。目前仅支持 Doctrine,但将支持其他 ORM,如 RedBean。
安装
通过 composer
$ composer require jetfirephp/db
对于 Doctrine 的使用,您必须要求 doctrine 包
$ composer require doctrine/orm
对于 RedBean 的使用,您必须要求 redbean 包
$ composer require gabordemooij/redbean
快速入门
// Require composer autoloader require __DIR__ . '/vendor/autoload.php'; // database config $options = [ 'driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'project', 'prefix' => 'jt_', 'path' => [__DIR__.'/'] ]; // Model facade $db = new \JetFire\Db\Doctrine\DoctrineModel($options); JetFire\Db\Model::init($db); // or for lazy loading // $db = [ // 'doctrine' => function() use ($options) { // new \JetFire\Db\Doctrine\DoctrineModel($options); // } // ] // JetFire\Db\Model::provide($db); // And for retrieve data you have 2 possible ways // 1) $accounts = Model::table('Account')->all(); // 2) $accounts = Account::all(); // Account.php must extends Model class
支持多个 ORM
// Require composer autoloader require __DIR__ . '/vendor/autoload.php'; // configuration $config = [ 'default' => [ 'driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'project', 'prefix' => 'jt_', 'path' => [__DIR__.'/'] ] ]; // set your orm provider $providers = [ 'doctrine' => function()use($config){ return new \JetFire\Db\Doctrine\DoctrineModel($config); }, 'redbean' => function()use($config){ return new \JetFire\Db\RedBean\RedBeanModel($config); }, 'pdo' => function()use($config){ return new \JetFire\Db\Pdo\PdoModel($config); }, ]; // pass the default orm in second argument or it will take the first given orm in providers JetFire\Db\Model::provide($providers,['orm'=>'pdo']); $account1 = Account::orm('doctrine')->select('lastName')->where('firstName','Peter')->get(); $account2 = Account::orm('pdo')->select('lastName')->where('firstName','Peter')->get(); $account3 = Account::orm('redbean')->select('firstName','lastName')->where('firstName','Peter')->orWhere('age','>',20)->get(); // you can also omit the orm method. The model will load the first orm provided (here doctrine). To load another orm by default you have to add the orm key in second argument of provide method $account3 = Account::select('firstName','lastName')->where('firstName','Peter')->orWhere('age','>',20)->get(); // will load doctrine orm
支持多种数据库
// Require composer autoloader require __DIR__ . '/vendor/autoload.php'; // configuration $config = [ 'db1' => [ 'driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'project1', 'prefix' => 'jt1_', 'path' => [__DIR__.'/'] ], 'db2' => [ 'driver' => 'mysql', 'host' => 'localhost', 'user' => 'root', 'pass' => '', 'db' => 'project2', 'prefix' => 'jt2_', 'path' => [__DIR__.'/'] ] ]; // set your orm provider $providers = [ 'doctrine' => function()use($config){ return new \JetFire\Db\Doctrine\DoctrineModel($config); }, 'redbean' => function()use($config){ return new \JetFire\Db\RedBean\RedBeanModel($config); }, 'pdo' => function()use($config){ return new \JetFire\Db\Pdo\PdoModel($config); }, ]; JetFire\Db\Model::provide($providers,['orm' => 'pdo', 'db' => 'default']); $account = Account::orm('doctrine')->db('db1')->all();
用法
让我们以一个账户表为例(这是一个 doctrine 表示例,但也可以与其他 ORM 一起使用)
/** * Class Account * @Entity * @Table(name="accounts") */ class Account extends Model{ /** * @Id * @Column(type="integer") * @GeneratedValue */ protected $id; /** * @Column(type="string",name="first_name",length=32) */ protected $first_name; /** * @Column(type="string",name="last_name",length=32) */ protected $last_name; /** * @Column(type="string",unique=true) */ protected $email; /** * @return mixed */ public function getFirstName() { return $this->first_name; } /** * @param mixed $firstName */ public function setFirstName($firstName) { $this->first_name = $firstName; } /** * @return mixed */ public function getId() { return $this->id; } /** * @param mixed $id */ public function setId($id) { $this->id = $id; } /** * @return mixed */ public function getLastName() { return $this->last_name; } /** * @param mixed $lastName */ public function setLastName($lastName) { $this->last_name = $lastName; } /** * @return mixed */ public function getEmail() { return $this->email; } /** * @param mixed $email */ public function setEmail($email) { $this->email = $email; } }
CRUD
CRUD 代表创建、读取、更新和删除。CRUD 操作是许多网络应用程序的核心。
创建
要从模型在数据库中创建新记录,您有两种方法
Account::create([ 'first_name' => 'Peter', 'last_name' => 'Parker', 'email' => 'peter.parker@spiderman.com' ]); // return true or false // Account::create()->with([...]) will also work
$account = Account::get(); // return a new account object $account->first_name = 'Peter'; // you can also use $account->setFirstName('Parker') or $account['first_name'] = 'Parker'; $account->last_name = 'Parker'; $account->email = 'peter.parker@spiderman.com'; $account->save();
检索与读取
加载所有账户
$accounts = Account::all();
要加载一个账户,只需传递您要查找的表的 ID
$account = Account::find(1);
如果您需要指定其他参数,您可以这样做
// with additional parameters $account = Account::where('id',1)->where('last_name','Parker')->get(); // or $account = Account::whereRaw('a.id = :id AND a.last_name = :last_name',['id' => 1, 'last_name' => 'Parker'])->get(); // select only some fields $account = Account::select('id','first_name')->where('id',1)->where('last_name','Parker')->get(); // return only id and first_name // order by $account = Account::orderBy('id','DESC')->get(); // count $account = Account::where('last_name','Parker')->count(); // return 1 // limit row $account = Account::take(2); // return only the first 2 accounts $account = Account::take(2,3); // return the 2 accounts after 3 rows
然后读取模型数据
$first_name = $account->first_name; // return 'Peter' // or $first_name = $account->getFirstName(); // return 'Peter' // or $first_name = $account['first_name']; // return 'Peter'
更新
要更新数据库中的模型,您可以用三种不同的方式来做
- 通过 ID 更新
Account::update(1)->with([ 'first_name' => 'Peter 2', ]);
- 使用条件更新
Account::where('id',1)->where('id',2)->set(['first_name' => 'Peter 2']);
- 首先检索表对象,然后设置您的字段
$account = Account::where('id',1)->get(); $account->first_name = 'Peter 2'; // you can also use $account->setFirstName('Parker') or $account['first_name'] = 'Parker'; $account->save();
删除
Account::destroy(1) // Deleting An Existing Model By Key Account::destroy(1,2) // multiple key supported // or Account::where('id',1)->delete() // or $account = Account::find(1); $account->delete();
原生 SQL
$account = Account::sql('select * form accounts where id = 1'); $account = Account::sql('select * form accounts where id = :id',['id' => 1]); // with parameters $account = Account::sql('update accounts set last_name = :last_name where id = :id',['last_name' => 'Parker 2','id' => 1]);
仓库
您可以为您的表创建一个仓库来处理自定义查询。您的仓库类名必须以 'Repository' 结尾
class AccountRepository { public function loadParker(){ return Account::where('last_name','Parker')->get(); } } // you have to add your repository path in your configuration $config = [ // previous config // ... 'repositories' => [ ['path' => 'repository_folder_path','namespace' => 'respository_namespace'] ], ]; $account = Account::repo()->loadParker();
许可证
JetFire Db 在 MIT 公共许可证下发布:https://open-source.org.cn/licenses/MIT。