-
Notifications
You must be signed in to change notification settings - Fork 2
3.3 Modules | Models
GrumpyCrouton edited this page Aug 7, 2022
·
7 revisions
Models are the layer of the module which is meant to do all of the database communications and work with data. You load them in your controller, and use the controller to call specific methods inside the model.
Inside your module folder, create a new folder called "models". Inside this folder create a new file called "new_model.php". The name of the model does not matter, I'm using this as an example.
new_model.php:
<?php
namespace modules\new_module\models;
class New_Model extends \Model {
public function example() {
return "This is an example";
}
}
In your controller, you'll want to load the model into a variable, then use the variable to interact with the model.
function get() {
//load model
$model = $this->model('new_model');
//use model
$return = $model->example();
echo $this->render('index');
}
In your model, it's easy to do database queries using GrumpyPDO, simply call it by using $this->db
.
For example, if you want to fetch all records from a certain table, just do this:
public function fetch_records() {
return $this->db->all("SELECT * FROM records");
}