Load Method

The load method purpose purpose is for loading a Model Class by generating (instantiating) a new property inside current controller class. The new property name will be same as model filename without .php extension in lowercase.

Basic Usage

Here is the basic usage load method from SENE_Controller class.

$this->load(string $model_filename [, string $alias]): void

Parameters

This method has 1 required parameter and 1 optional parameter.

$model_filename

The $model_filename value can be contain a string of model filename. File location always relatives to app/model directory. So, if we have a model class under app/model/api/hello_model.php We have to fill the first parameter with extra suffix path, like api/hello_model.

$alias

The $alias value can be contain a string that override the name of property that instantiated by this method.

Example

On this example, will show the implementation of load method without an alias.

<?php
class Blog extends SENE_Controller {
  public function __construct(){
    parent::__construct();
    $this->load('hello_model');
  }
  public function index(){
    //executing the hello_model object
    print_r($this->hello_model->get());
    die();
  }
}

With Alias

On this example, will show the implementation of load method with an alias.

<?php
class Blog extends SENE_Controller {
  public function __construct(){
    parent::__construct();
    $this->load('api/hello_model','hm');
  }
  public function index(){
    //executing the hello_model object
    print_r($this->hm->get());
    die();
  }
}

Tips

If you confused how to fill the alias name, you can get from first letter of each class name.

example, app/model/api/hello_detail_model2.php and then the alias will be hdm2.

Inside a Directory

On this example, will show the implementation of load method loading a model inside a directory and without an alias.

<?php
class Blog extends SENE_Controller {
  public function __construct(){
    parent::__construct();
    $this->load('api/hello_model');
  }
  public function index(){
    //executing the hello_model object
    print_r($this->hello_model->get());
    die();
  }
}

Caution

While loading the model from sub directory, the path prefix will not instantiate as object model name.

So, be careful for choosing the naming class model or alias.

If necessary, you can duplicate a model class to avoid conflict with extra suffix with number.

example, app/model/api/hello_model2.php and then the class name Hello_Model2.