Skip to content

phpcombi/eloquent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Combi Eloquent

Eloquent support for PHP\Combi Framework.

How to use

  1. Create combi project(if not)
composer create-project combi/combi=dev-master myapp
  1. Require combi/eloquent
composer require combi/eloquent=dev-master
  1. Configure database connection
mkdir src/config/eloquent/databases.neon
vim src/config/eloquent/databases.neon

Enter connection config like this:

default:
    driver:     mysql
    host:       127.0.0.1
    port:       3306
    database:   mydb
    username:   root
    password:   123456
    charset:    utf8mb4
    collation:  utf8mb4_general_ci
    prefix:     ''
  1. Try to use
use Combi\Eloquent as DB;

foreach (DB::table('users')->get() as $entry) {
    helper::du($entry->toArray());
}
  1. Use entity

Declare entity:

use Combi\{
    Helper as helper,
    Abort as abort,
    Runtime as rt
};

use Illuminate\Database\Eloquent\SoftDeletes;

/**
 *
 *
 * @property int $id
 * @property string $name
 * @property string $pass
 * @property string $email
 * @property string $nickname
 * @property int $gender
 * @property string $birthday
 */
class User extends Combi\Eloquent\Entity
{
    use SoftDeletes;

    protected $table    = 'users';
    protected $dates    = ['deleted_at'];

    protected $fillable = [
        'email',
        'nickname',
        'gender',
        'birthday',
    ];
    protected $casts = [
        'id'        => 'integer',
        'name'      => 'string',
        'pass'      => 'string',
        'email'     => 'string',
        'nickname'  => 'string',
        'gender'    => 'integer',
        'birthday'  => 'datetime',
    ];
}

And use it:

$user = new User();
$user->name     = 'abc'.mt_rand(10000, 99999);
$user->pass     = md5('123123');
$user->email    = $user->name.'@xxx.com';
$user->nickname = $user->name.'-nick';
$user->gender   = mt_rand(1, 2);
$user->birthday = '2017-10-11 '.date('H:i:s');
$user->save();

$user2 = User::query()->where('name', 'somename')->first();

Use Cabin

Cabin is an entry container in a combi action. It will control entry load only once in an action, and will auto save the changes when action is done.

like this:

$user = User::find(2);
$user->name = 'def'.mt_rand(10000, 99999);
// and it will auto save when action done.

for ($i = 0; $i < 1000; $i++) {
    User::find(1);
}
// it will load once by db.

If you do not want to use cabin, make entity proporty protected $_cabin_id=null.

If want disable auto release feature, make config file src/config/eloquent/settings.neon like:

cabin:
    auto_release:
        #-   0   # release default cabin when action done

About

eloquent support for combi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages