Skip to content
やかみそら edited this page May 5, 2017 · 11 revisions

Model

The model is optional for those who want to use a more traditional MVC model.

What is a model?

The model is specifically a PHP class used to work with the database. For example, if you want to write a blog, then you should have a model class for inserting, updating, and getting blog data. Here is an example of a model class:

namespace app\models;

use Kotori\Core\Model;

class Blog extends Model {

    public $title;
    public $content;
    public $date;

    public function __construct()
    {
        parent::__construct();
    }

    public function getUsers()
    {
        $query = $this->db->select("account", "*");
        return $query;
    }
}

Analysis model

The model class is located in your app/models directory. The specific file is named {Modelname}

The basic prototype of the model class is:

namespace app\models;

use Kotori\Core\Model;

class {Modelname} extends Model {

    public function __construct()
    {
        parent::__construct();
    }

}

Where {Modelname} is the name of the class, making sure your class inherits the \Kotori\Core\Model base class.

Loading Models

Your model will typically be loaded and called in your controller's method, and you can use the following methods to load the model:

$this->model->Blog->doSomeThing();

Here is an example where the controller loads a model and processes a view:

namespace app\controllers;

use Kotori\Core\Controller;

class Blog extends Controller {

    public function blog()
    {
        $user_list = $this->model->Blog->getUsers();
        $this->view->assign('list',$user_list);
        $this->view->display();       
    }
}

The model is as follows:

namespace app\models;

use Kotori\Core\Model;

class Blog extends Model
{
    public function getUsers()
    {
        return 'something';
    }
}
Clone this wiki locally