Nxdom Framework

Nusantara eXtreme Development Object Model — dokumentasi resmi

Advanced Features

NexaModel menyediakan method-method generic untuk operasi CRUD yang lebih sederhana:

NexaModel · Storage() · NexaDb

$this->Storage('news')->where()->first();

Generic Record Operations

NexaModel menyediakan method-method generic untuk operasi CRUD yang lebih sederhana:

Insert Record

// Insert record dengan auto timestamps $userId = $this->insertRecord('users', [ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => password_hash('secret', PASSWORD_DEFAULT) ]); // created_at dan updated_at akan otomatis ditambahkan echo "User created with ID: " . $userId;

Update Record

// Update record berdasarkan ID $success = $this->updateRecord('users', [ 'name' => 'John Doe Updated', 'email' => 'john.updated@example.com' ], 1); // updated_at akan otomatis diperbarui if ($success) { echo "User updated successfully"; }

Delete Record

// Delete record berdasarkan ID $success = $this->deleteRecord('users', 1); if ($success) { echo "User deleted successfully"; }

Find Record

// Find record berdasarkan ID $user = $this->findRecord('users', 1); if ($user) { echo "User found: " . $user['name']; } else { echo "User not found"; }

Raw Queries

// Raw query dengan binding $users = $this->raw( "SELECT * FROM users WHERE status = ? AND created_at > ?", ['active', '2024-01-01'] );

Transactions

$result = $this->transaction(function() use ($model) { // Insert user $userId = $this->Storage('users')->insert([ 'name' => 'John Doe', 'email' => 'john@example.com' ]); // Insert profile $this->Storage('profiles')->insert([ 'user_id' => $userId, 'bio' => 'User biography' ]); return $userId; });

Union Queries

$activeUsers = $this->Storage('users') ->where('status', 'active') ->select(['id', 'name', 'email']); $inactiveUsers = $this->Storage('users') ->where('status', 'inactive') ->select(['id', 'name', 'email']); $allUsers = $activeUsers->union($inactiveUsers)->get();

Distinct

$uniqueEmails = $this->Storage('users') ->distinct('email') ->get();

Exists

$hasActiveUsers = $this->Storage('users') ->where('status', 'active') ->exists();

Helper Methods

Null Value Handling

// Handle null values dengan default $users = $this->Storage('users') ->get(); $processedUsers = $this->handleNullValues($users, [ 'name' => 'Unknown', 'email' => 'no-email@example.com', 'status' => 'inactive' ]);

Slug Generation

$slug = $this->addSlug('This is a Title'); // Result: 'this-is-a-title'

JSON Handling

$json = $this->toJson($data, true); // pretty print

Select with Defaults

// Select dengan default values menggunakan COALESCE $users = $this->Storage('users') ->selectWithDefaults([ 'id' => null, 'name' => null, 'status' => 'inactive', 'email' => null, 'avatar' => '/assets/images/default-avatar.png' ]) ->get();

Query Shortcuts

Latest/Oldest

// Get latest records (berdasarkan created_at) $latestUsers = $this->Storage('users') ->latest() ->limit(10) ->get(); // Get oldest records $oldestUsers = $this->Storage('users') ->oldest() ->limit(10) ->get(); // Latest berdasarkan kolom tertentu $latestPosts = $this->Storage('posts') ->latest('published_at') ->get();

First or Fail

// Get first record atau throw exception jika tidak ada try { $user = $this->Storage('users') ->where('email', 'john@example.com') ->firstOrFail(); } catch (\Exception $e) { echo "User not found!"; }

Advanced Aggregation Methods

Count Multiple Where

// Count dengan multiple where conditions $stats = $this->Storage('users') ->countMultipleWhere([ 'active_users' => [ ['status', '=', 'active'], ['verified', '=', 1] ], 'inactive_users' => [ ['status', '=', 'inactive'] ], 'unverified_users' => [ ['verified', '=', 0] ] ]); // Result: ['active_users' => 120, 'inactive_users' => 30, 'unverified_users' => 15]

Quick Status Count

// Quick count berdasarkan status $statusCount = $this->Storage('users') ->quickStatusCount('status', ['active', 'inactive', 'banned']); // Result: ['active' => 150, 'inactive' => 25, 'banned' => 5]

Get Percentages

// Get percentages untuk kondisi tertentu $percentages = $this->Storage('users') ->getPercentages('status', [ 'active' => ['status', '=', 'active'], 'inactive' => ['status', '=', 'inactive'] ], 2); // 2 decimal places // Result: ['active' => 85.71, 'inactive' => 14.29]

Display Methods

NexaModel menyediakan method untuk menampilkan hasil query dengan formatting:

Get and Display

// Display hasil query dengan format yang rapi $this->Storage('users') ->where('status', 'active') ->limit(5) ->getAndDisplay(true); // dengan <pre> tag

First and Display

// Display record pertama $this->Storage('users') ->where('id', 1) ->firstAndDisplay(true);

Count and Display

// Display count dengan format $this->Storage('users') ->where('status', 'active') ->countAndDisplay(true);

Count by Conditions and Display

// Display count by conditions $this->Storage('users') ->countByConditionsAndDisplay('status', [ 'active' => ['status', '=', 'active'], 'inactive' => ['status', '=', 'inactive'] ], true);

Count with Percentage and Display

// Display count dengan percentage $this->Storage('users') ->countWithPercentageAndDisplay('status', ['active', 'inactive'], true);

Aggregate and Display

// Display aggregation results $this->Storage('orders') ->aggregateAndDisplay(['total_amount', 'tax_amount'], 'sum', true);

Lanjut

Topik sebelumnya: Database Management. Topik berikutnya: Examples. Lihat juga Models, Events, Helper & template, daftar dokumentasi.