accolon/izanami

数据层抽象库

5.5.0 2021-03-03 23:15 UTC

README

配置

# config.php

define("DB_CONFIG", [
    "driver" => "mysql",
    "host" => "localhost",
    "port" => 3306,
    "name" => "accolon",
    "charset" => "utf8",
    "user" => "accolon",
    "password" => "password"
]);

模型

use Accolon\DataLayer\Model;

class User extends Model
{
    protected string $table = "users";

    protected $sentives = [
        "password"
    ];
}

插入

// First way
$user = new User();

$user->name = "Accolon";
$user->email = "test@gmail.com";

$user->save();

// Second way
$user = new User([
    "name" => "Accolon",
    "email" => "test@gmail.com"
]);

$user->save();


// Third way
$user = new User();

$user->create([
    "name" => "Accolon",
    "email" => "test@gmail.com"
]);

更新

$user = new User();

$user->where("name", "Accolon")->update([
    "email" => "test2@gmail.com"
]);

// Or
$user = (new User)->find(1);
// $user->name == "Accolon"
$user->email = "email" => "test@gmail.com";
$user->save();

删除

$user = new User();

$user->where("name", "Accolon")->delete();

// Or

$user->name = "Accolon";
$user->delete();

查询

获取

$table = new User();

// Return one element
$user = $table->where("name", "Accolon")->first();

获取全部

$table = new User();

// Return array
$user = $table->where("id", ">", 1)->all();

在哪里

$table = new User();

$table->where("name", "Accolon");

// Equal

$table->where("name", "=", "Accolon");

// Other compares

$table->where("id", ">", 1);

$table->where("id", "<", 1);

// Multiple wheres

$table->where("name", "=", "Accolon")->where("id", 2);

// whereOr

$table->whereOr("id", 1)->whereOr("name", "Accolon");

// Where In
$table->whereIn('id', [1, 2, 3]);

查找

$table = new User();

$user = $table->find(1);

查找或失败

$table = new User();

try {
    $user = $table->findOrFail(1);
} catch (\Exception $e) {
    die("Not found");
}

第一个

$table = new User();

$user = $table->where("id", ">", 2)->first();

全部

$table = new User();

$user = $table->all();

$table = new User();

$users = $table->where("id", ">", 2)->order("id", "DESC")->getAll();

$user = $table->where("id", ">", 2)->desc()->all();

$user = $table->where("id", ">", 2)->asc()->all();

限制

$table = new User();

$user = $table->where("id", ">", 2)->limit(5)->getAll();

计数

$table = new User();

$user = $table->where("id", ">", 2)->count();

关系

use Accolon\Izanami\Model;

class User extends Model
{
    // One to One
    public function phone()
    {
        return $this->hasOne(Phone::class);
    }

    // One to Many
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
}

class Post extends Model
{
    // One to Many (Inverse)
    public function user()
    {
        return $this->belongsToOne(User::class);
    }

    // Many to Many
    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }
}

class Phone extends Model
{
    // One to One (Inverse)
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

class Tag extends Model
{
    // Many to Many (Inverse)
    public function posts()
    {
        return $this->morphedByMany(Post::class, 'taggable');
    }
}

原始

$table = new User();

// Return boolean
$result = DB::raw("SELECT * FROM test WHERE id = 1");

// Return array
$result = DB::selectRaw("SELECT * FROM test WHERE id = 1");