PHP code example of accolon / izanami

1. Go to this page and download the library: Download accolon/izanami library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

accolon / izanami example snippets


# 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 = "[email protected]";

$user->save();

// Second way
$user = new User([
    "name" => "Accolon",
    "email" => "[email protected]"
]);

$user->save();


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

$user->create([
    "name" => "Accolon",
    "email" => "[email protected]"
]);

$user = new User();

$user->where("name", "Accolon")->update([
    "email" => "[email protected]"
]);

// Or
$user = (new User)->find(1);
// $user->name == "Accolon"
$user->email = "email" => "[email protected]";
$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");