2017 © Pedro Peláez
 

library real-rap

Codeigniter ORM for 3.0 or higher

image

csitc/real-rap

Codeigniter ORM for 3.0 or higher

  • Friday, September 23, 2016
  • by wugang8068
  • Repository
  • 2 Watchers
  • 17 Stars
  • 60 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 2 Versions
  • 9 % Grown

The README.md

RealRap

This is a CI ORM, and inspired by laravel eloquent, (*1)

中文地址: https://github.com/wugang8068/RealRap/blob/master/ReadMe_zh.md, (*2)

How to install:

composer require csitc/real-rap dev-master

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, (*3)

    $users = User::all(['*'])->where([
          'user_id <=' => 50]
      )->order([
          'user_id' => 'desc',
          'user_mobile' => 'desc'])
      ->limit(1)->getOne();
  

The result maybe like this:, (*4)

{
    "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:, (*5)

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, (*6)

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, 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;, (*7)

First, get the user record, (*8)

$this->user = User::all(['*'])->where([
            'user_mobile' => '17010058640'
        ])->order([
            'user_id' => 'desc',
            'user_mobile' => 'desc'
        ])->limit(1)->getOne();

Then fetch if by the flowing:, (*9)

$keys = $this->user->key  
//The $keys is an array within objects or an object or null depends on the relation in User.php

The model can be write in this:, (*10)

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:, (*11)

  • Wrap the collection data instead of an array

The Versions

23/09 2016

dev-master

9999999-dev

Codeigniter ORM for 3.0 or higher

  Sources   Download

MIT

The Requires

  • php >=5.5.30

 

by Chart

orm ci

23/09 2016

1.0.0

1.0.0.0

Codeigniter ORM for 3.0 or higher

  Sources   Download

MIT

The Requires

  • php >=5.5.30

 

by Chart

orm ci