Kotchasan Framework Documentation
Model Class - Database Access Object
Model Class - Database Access Object
The Model class serves as the base abstract class for all data models. It simplifies database interactions by acting as a bridge to Kotchasan\Database and various Query Builders.
Namespace
Kotchasan\ModelUsage
Models typically extend this class to encapsulate business logic and database queries.
class ProductModel extends \Kotchasan\Model
{
// Override connection name if needed
protected $conn = 'warehouse_db';
public function findProduct($id)
{
return $this->select()->from('products')->where(['id', $id])->first();
}
}Properties
$db
Holds the active Kotchasan\Database instance.
- Visibility:
protected
$conn
The configuration key for the database connection.
- Default:
'default' - Visibility:
protected(Overrideable)
Static Methods
create()
Factory method to instantiate the model.
public static function create(): staticcreateQuery()
Create a Query Builder instance directly from the model.
public static function createQuery(): \Kotchasan\QueryBuilder\QueryBuilderInterfacecreateDb()
Instantiate and return the underlying Database driver.
public static function createDb(): DatabasePublic Methods
Transaction Management
Control database transactions manually.
public function beginTransaction(): bool
public function commit(): bool
public function rollback(): boolQuery Builder Factories
Methods to initiate fluent query construction.
select()
Start a SELECT query.
public function select(mixed ...$columns): SelectBuilderUsage:
$q = $model->select('id', 'name');
// OR
$q = $model->select(); // SELECT *insert()
Start an INSERT query.
public function insert(string $table): InsertBuilderupdate()
Start an UPDATE query.
public function update(string $table): UpdateBuilderdelete()
Start a DELETE query.
public function delete(string $table): DeleteBuilderDatabase Helpers
raw()
Execute a raw SQL command.
public function raw(string $sql, array $params = []): mixedlastInsertId()
Get the auto-generated ID of the last INSERT operation.
public function lastInsertId(): int|stringgetTableName()
Get the physical table name (including configured prefix).
public function getTableName(string $table): stringgetPrefix()
Get the configured table prefix.
public function getPrefix(): stringgetDB()
Access the raw Database instance.
public function getDB(): DatabaseFile Size: 216 lines