基于CI3.0 开发的ORM,操作数据更加简单, 抛弃CI_Model
2016-09-23 20:01
393 查看
Github: https://github.com/wugang8068/RealRap
安装方法:
欢迎大家进行fork, 如果给我一个star, 我会更加努力维护的哦;
ORM有什么好处, 想必各位都大概知道了, ORM可以方便我们查找数据更加便捷,只需要去维护模型之间的关系, 而不需要去处理各种复杂的SQL语句,RealRap彻底抛弃CI_Model, 使用更抽象的\RealRap\Model作为模型父类。 底层的sql查询还是采用CI_DB_query_builder的AR。可以看做是对DB的一层封装吧。 相对于Laravel的Eloquent来说,无需配置Connection,直接复用CI的DB Connection。目前支持单个模型的CRUD,还不支持模型关联等复杂的关系逻辑。后面会慢慢完善这个项目。我是一名PHP小菜,还请各位大神勿喷,缺点还请大家赐教。
以下是英文介绍, 鄙人英文很low ,将就看一下吧;
The result mybe like this:
In the User.php, we can just write like this:
or
4000
for example, there is a table named ==inf_user==, and a table named ==inf_cd_keys==, and each recored in ==inf_user== has one or many record inf ==inf_cd_keys==, so it’s easy to access the result with the flowing code;
First, get the user record
Then fetch if by the flowing:
Or the retrieve code can like this
User.php
Key.php
To DO LIST:
Wrap the collection data instead of an array
安装方法:
composer require csitc/real-rap dev-master
欢迎大家进行fork, 如果给我一个star, 我会更加努力维护的哦;
ORM有什么好处, 想必各位都大概知道了, ORM可以方便我们查找数据更加便捷,只需要去维护模型之间的关系, 而不需要去处理各种复杂的SQL语句,RealRap彻底抛弃CI_Model, 使用更抽象的\RealRap\Model作为模型父类。 底层的sql查询还是采用CI_DB_query_builder的AR。可以看做是对DB的一层封装吧。 相对于Laravel的Eloquent来说,无需配置Connection,直接复用CI的DB Connection。目前支持单个模型的CRUD,还不支持模型关联等复杂的关系逻辑。后面会慢慢完善这个项目。我是一名PHP小菜,还请各位大神勿喷,缺点还请大家赐教。
以下是英文介绍, 鄙人英文很low ,将就看一下吧;
RealRap
This is a CI ORM, and inspired by laravel eloquentRetrieve
Example: We need to fetch the user where user_id >= 50 and order by user_id and user_mobile and get the first one$users = User::all(['*'])->where([ 'user_id <=' => 50] )->order([ 'user_id' => 'desc', 'user_mobile' => 'desc']) ->limit(1)->getOne();
The result mybe like this:
{ "id":50, "user_nick_name":"18386053521", "user_device_id":"B86E62AC-5FC4-45E3-A3F0-EB4544DB135D", "user_mobile":"17288", "user_create_date":"2016-07-10 09:44:54" }
In the User.php, we can just write like this:
class User extends Model{ protected $table = 'inf_user'; protected $primaryKey = 'user_id'; protected $cast = [ 'user_id' => 'integer', 'is_subscribed' => 'bool' ]; protected $hidden = [ 'agent_user', 'bank_real_name', 'is_subscribed', 'rebate_already_mentioned', 'rebate_being_mention', 'rebate_unmentioned', 'user_email' ]; protected $attributes = [ 'user_id' => 'id', ]; }
Update
$user = User::findWhere([ 'user_mobile' => '12381121695' ])->getOne(); if($user){ $user->user_mobile = '134234'; $user->save(); }
Create
$user = new User(); $user->user_nick_name = 'Tom'; $user->save();
Delete
$user = User::findWhere(['user_mobile' => '18600908262'])->getOne(); if($user){ print_r($user->delete() ? 'record delete success' : 'record delete failed'); }else{ print_r('record is not exists'); }
or
User::findWhere(['user_mobile' => '18600908262'])->delete()
Transaction
\RealRap\RealRap::trans(function(){ $user = new User(); $user->user_mobile = '13345727773'; $user->save(); $user = new User(); $user->user_mobile = '13347818106'; $user->save(); },function(){ echo 'success'; },function(){ echo 'error'; });
Model Relation
if we want to add model relation,4000
for example, there is a table named ==inf_user==, and a table named ==inf_cd_keys==, and each recored in ==inf_user== has one or many record inf ==inf_cd_keys==, so it’s easy to access the result with the flowing code;
First, get the user record
$this->user = User::all(['*'])->where([ 'user_mobile' => '17010058640' ])->order([ 'user_id' => 'desc', 'user_mobile' => 'desc' ])->limit(1)->getOne();
Then fetch if by the flowing:
$keys = $this->user->key //The $keys is an array within objects or an object or null depends on the relation in User.php
Or the retrieve code can like this
$this->user = User::all(['*'])->with(['key','order'])->where([ 'user_id < ' => 20 ])->order([ 'user_id' => 'desc', 'user_mobile' => 'desc' ])->get(); dump($this->user);
User.php
class User extends Model { protected $table = 'inf_user'; protected $primaryKey = 'user_id'; protected function key(){ return $this->hasMany(Key::class,'cdk_bind_user'); //return $this->hasOne(Key::class,'cdk_bind_user'); } }
Key.php
class Key extends Model { protected $table = 'inf_cd_keys'; protected $primaryKey = 'cdk_id'; }
To DO LIST:
Wrap the collection data instead of an array
相关文章推荐
- 自定义封装简单的基于FMDB以及数据model的数据库操作
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台(C#开发)-AE常用操作代码
- iOS开发JSON文件解析数据成Model的过程简单介绍
- afinal - afinal 是一个android的 orm 和 ioc 框架。通过afinal的finalAcitivity,finalBitmap,finalDB,finalHttp,开发android应用将更加简单。 - Google Project Hosting
- Laravel ORM 数据model操作
- TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
- Android开发7:简单的数据存储(使用SharedPreferences)和文件操作
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 数据关系映射ORM
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- 基于Python项目的Redis缓存消耗内存数据简单分析(附详细操作步骤)
- jQuery基于ajax操作json数据简单示例
- iOS开发中大部分App的网络数据交换是基于HTTP协议的。本文将简单介绍在Swift中使用HTTP进行网络请求的几种方法。
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台(C#开发)-AE常用操作代码
- 基于ArcGIS10.0和Oracle10g的空间数据管理平台(C#开发)-AE常用操作代码
- 基于Castle ActiveRecord开发Domain Model详解(一)对象关系到数据表的映射
- Spring 让 LOB 数据操作变得简单易行
- 为了开发省力一点-基本数据操作
- 终于可以让Flash基于C#完美操作数据库了,想开发全flash的,全部读取数据库的个人BLOG...
- JDBC连接并简单操作数据库存(自己学习用)
- asp快速开发方法之数据操作实例代码第1/3页