TrishulApi\Core\Data\Model

Namespace: TrishulApi\Core\Data
Type: abstract class

Overview

The Model class is the base class for all database models in the Trishul API framework. It provides a rich set of static methods for CRUD operations, querying, relationships, and raw SQL execution. Extend this class to represent your database tables and interact with them using an expressive, object-oriented API.

Usage Example


use TrishulApi\Core\Data\Model;

class User extends Model {
    public static string $table_name = 'users';
    public static string $primary_key = 'id';
}

// Fetch all users
$users = User::all();

// Find a user by ID
$user = User::getById(1);

// Create a new user
$newId = User::create(['name' => 'Alice', 'email' => 'alice@example.com']);

// Update a user
User::update(1, ['email' => 'alice@newdomain.com']);

// Delete a user
User::delete(1);

// Soft delete a user
User::softDelete(2);

// Find users with conditions
$users = User::where(['status = "active"'], 20, 0);

// Count users
$count = User::count(['status = "active"']);

// Check if a user exists
$exists = User::exists(['email = "alice@example.com"']);

// Raw query
$results = User::rawQuery('SELECT * FROM users WHERE status = :status', ['status' => 'active']);
        

Properties

PropertyTypeDescription
public static string $table_name string Name of the database table (must be set in child class).
public static string $primary_key string Name of the primary key column (default: id).

CRUD Methods

public static function all(): array

Returns all records from the table.

public static function getById($id): ?array

Returns a single record by its primary key.

public static function create(array $data): int

Inserts a new record and returns the new record's ID.

public static function update($id, array $data): bool

Updates a record by its primary key.

public static function delete($id): bool

Deletes a record by its primary key.

public static function softDelete($id): bool

Performs a soft delete by setting deleted_at timestamp.

Query Methods

public static function find($conditions = [], $limit = 10, $offset = 0): array

Finds records matching the given conditions, with limit and offset.

public static function where($conditions = [], $limit = 10, $offset = 0): array

Alias for find(). Finds records matching the given conditions.

public static function filter($conditions = [], $limit = 10, $offset = 0): array

Alias for find(). Finds records matching the given conditions.

public static function count($conditions = []): int

Counts the number of records matching the given conditions.

public static function exists($conditions = []): bool

Checks if any records exist matching the given conditions.

Raw SQL Methods

public static function rawQuery($query, $params = []): array

Executes a raw SQL query and returns all results.

public static function rawQuerySingle($query, $params = []): ?array

Executes a raw SQL query and returns a single record.

public static function rawQueryCount($query, $params = []): int

Executes a raw SQL query and returns the count of records.

public static function rawQueryExists($query, $params = []): bool

Executes a raw SQL query and checks if any records exist.

Transaction Support

public static function transaction(callable $callback): mixed

Executes a set of database operations within a transaction. Rolls back if an exception occurs.

Relationship Methods

public static function hasMany($relatedModel, $foreignKey, $localKey = 'id'): array

Returns all related records (one-to-many relationship).

public static function belongsTo($relatedModel, $foreignKey, $localKey = 'id'): ?array

Returns a single related record (many-to-one relationship).

public static function hasOne($relatedModel, $foreignKey, $localKey = 'id'): ?array

Returns a single related record (one-to-one relationship).

public static function hasManyWithConditions($relatedModel, $foreignKey, $conditions = [], $localKey = 'id'): array

Returns related records with additional conditions.

public static function belongsToWithConditions($relatedModel, $foreignKey, $conditions = [], $localKey = 'id'): ?array

Returns a related record with additional conditions.

public static function hasOneWithConditions($relatedModel, $foreignKey, $conditions = [], $localKey = 'id'): ?array

Returns a single related record with additional conditions.

Utility Methods

public static function getTableName(): string

Returns the table name for the model.

public static function getPrimaryKey(): string

Returns the primary key column name.

public static function getModelClass(): string

Returns the fully qualified class name of the model.

public static function queryBuilder(): QueryBuilder

Returns a new QueryBuilder instance for advanced queries.

Notes

Related Classes

Changelog