本帖最后由 wugang8068 于 2016-10-13 13:47 编辑
Github: https://github.com/wugang8068/RealRap
安装方法:
PHP复制代码 composer require csitc/real-rap 复制代码 Github
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
PHP复制代码 $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:
JS复制代码 {
"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:
PHP复制代码 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
PHP复制代码 $user = User::findWhere([
'user_mobile' => '12381121695'
])->getOne();
if($user){
$user->user_mobile = '134234';
$user->save();
}
复制代码
### Insert
PHP复制代码 $user = new User();
$user->user_nick_name = 'Tom';
$user->save(); 复制代码
### Delete
PHP复制代码 $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
PHP复制代码 User::findWhere(['user_mobile' => '18600908262'])->delete()
复制代码
### Transaction
PHP复制代码 \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
PHP复制代码 $this->user = User::all(['*'])->where([
'user_mobile' => '13000000000'
])->order([
'user_id' => 'desc',
'user_mobile' => 'desc'
])->limit(1)->getOne(); 复制代码
Then fetch if by the flowing: PHP复制代码 $keys = $this->user->key //The $keys is an array within objects or an object or null depends on the relation in User.php
[p =24, null, left ] 复制代码
Or: PHP复制代码 $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
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
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
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
|