accolon/izanami

Library to data layer abstration

5.5.0 2021-03-03 23:15 UTC

README

Config

# config.php

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

Model

use Accolon\DataLayer\Model;

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

    protected $sentives = [
        "password"
    ];
}

Insert

// 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"
]);

Update

$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();

Delete

$user = new User();

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

// Or

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

Query

Get

$table = new User();

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

Get All

$table = new User();

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

Where

$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]);

Find

$table = new User();

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

Find Or Fail

$table = new User();

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

First

$table = new User();

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

All

$table = new User();

$user = $table->all();

Order By

$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();

Limit

$table = new User();

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

Count

$table = new User();

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

Relationships

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');
    }
}

Raw

$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");