accolon / izanami
Library to data layer abstration
5.5.0
2021-03-03 23:15 UTC
Requires
- accolon/logging: ^0.1.0
Requires (Dev)
- pestphp/pest: ^1.0
- dev-master
- 5.5.0
- 5.4.1
- 5.4.0
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 4.5.1
- 4.5.0
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.0
- 3.4.1
- 3.4.0
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0
- 2.9.0
- 2.8.0
- 2.7.0
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.0.0
This package is auto-updated.
Last update: 2024-10-29 05:59:38 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");