基于CI3.0 开发的ORM,操作数据更加简单, 抛弃CI_Model
本帖最后由 wugang8068 于 2016-10-13 13:47 编辑Github: https://github.com/wugang8068/RealRap
安装方法:
composer require csitc/real-rapGithub
ORM有什么好处, 想必各位都大概知道了, ORM可以方便我们查找数据更加便捷,只需要去维护模型之间的关系, 而不需要去处理各种复杂的SQL语句,RealRap彻底抛弃CI_Model, 使用更抽象的\RealRap\Model作为模型父类。 底层的sql查询还是采用CI_DB_query_builder的AR。可以看做是对DB的一层封装吧。 相对于Laravel的Eloquent来说,无需配置Connection,直接复用CI的DB Connection。目前支持单个模型的CRUD,以及模型关联, 一对一, 一对多关联。后面会慢慢完善这个项目。我是一名PHP小菜,还请各位大神勿喷,缺点还请大家赐教。
# _RealRap_
This is a CI ORM, and inspired by Laravel Eloquent
### Retrieve
Example: We need to fetch the user where user\_id >= 50 and order by user\_id and user\_mobile and get the first one
$this->user = User::all(['*'])->with(['key','order'])->where([
'user_id < ' => 20
])->order([
'user_id' => 'desc',
'user_mobile' => 'desc'
])->get();
dump($this->user);
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();
}
### Insert
$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 Relationif we want to add model relation, 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' => '13000000000'
])->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:$this->user = User::all(['*'])->with(['key','order'])->where([
'user_id < ' => 20
])->order([
'user_id' => 'desc',
'user_mobile' => 'desc'
])->get();
dump($this->user);
The model can be write in this:
User.php
namespace App\models;
use RealRap\Model\Model as Model;
/**
* Class User
*
* @package App\Models\PostLoanModel
* @property integer $user_id
* @property string $user_nick_name
* @property string $user_device_id
* @property string $user_mobile
* @property string $user_password
* @property string $user_create_date
* @mixin User
*/
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',
];
public function key(){
return $this->hasMany(Key::class,'cdk_bind_user');
//return $this->hasOne(Key::class,'cdk_bind_user');
}
public function order(){
return $this->hasOne(Order::class,'related_user');
}
}
Key.php
namespace App\models;
use RealRap\Model\Model as Model;
class Key extends Model
{
protected $table = 'inf_cd_keys';
protected $primaryKey = 'cdk_id';
protected $hidden = [
'cdk_belong_agent',
'cdk_bind_user',
'cdk_expire_time',
'has_been_sold',
'comments',
'is_deleted'
];
}
Order.php
namespace App\models;
use RealRap\Model\Model;
class Order extends Model
{
protected $table = 'wx_inf_order';
protected $primaryKey = 'index_id';
}
To DO LIST:
* Wrap the collection data instead of an array
赞!已 Star! Hex 发表于 2016-9-9 12:10
赞!已 Star!
多谢hex大神鼓励 有点像laravel的ORM,不过好厉害,laravel的我看了好几遍才理解一点点 那么重口,你可以去用TP,干嘛要改CI... 不明觉厉!:L:L:L 很不错,用的很顺手,类似laravel的ORM的感觉,已star
页:
[1]