The framework is mvc based, which stands for model, view, controller. The mvc design pattern is build with this structure where, models are used for database interaction, views for the html markup and controllers for the main/core logic of the webapplication. Commonly, controllers return views and model instances are created inside controllers.
Models can be created inside the /app/models folder.
<?php
namespace app\models;
use database\DB;
class ExampleModel {
public function example() {
$data = DB::try()->all('table')->fetch();
return $data;
}
}
Inside the base model there are a couple of methods you could use to interact with the database alternatively.
In this table you'll find information about the different methods you can use and what kind of data can be passed as an argument.
Method | Arg | Type | Expect |
---|---|---|---|
get() | 1 | string | an id |
getColumns() | 1 | column names | array |
getColumns() | 2 | string | an id |
where() | 1 | array | column name, value |
whereColumns() | 1 | array | column names |
whereColumns() | 2 | array | column name, value | insert() | 1 | array associative | columns names, values |
update() | 1 | array | column name, value |
update() | 2 | array | column names, values |
delete() | 1 | string | column name |
delete() | 2 | string | value |
To use the base model methods, register the models inside the app/models/register/Tables.php file. Inside the tables property, add the model name without the php extentsion as the key value and the database table name as the value.
<?php
namespace app\controllers;
class Tables {
public $tables [
'Model' => 'table',
];
}
Call the get function method to fetch a row on id.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
$data = ExampleModel::get('id');
}
}
Call the getColumns function method to fetch a row on id but only fetch data from preffered columns.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
$data = ExampleModel::getColumns(['column', 'column'], 'id');
}
}
Call the where function method to fetch data on condition.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
$data = ExampleModel::where(['column' => 'value']);
}
}
Call the whereColumns function method to fetch data on condition but only preffered column data.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
$data = ExampleModel::whereColumns(['column', 'column'], ['column' => 'value');
}
}
Call the insert function method to store data.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
ExampleModel::insert([
'column' => 'value',
'column' => 'value',
'column' => 'value',
'column' => 'value'
)];
}
}
Call the update function method to update data.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
ExampleModel::update(['column' => 'value'], [
'column' => 'value',
'column' => 'value',
'column' => 'value',
'column' => 'value'
)];
}
}
Call the delete function method to delete a row on condition.
<?php
namespace app\controllers;
use app\models\ExampleModel;
class ExampleController {
public function example() {
ExampleModel::delete('column', 'value');
}
}