Creation of Model and first steps to use
The Model typically focuses on interacting with the database and encapsulating data logic, which can help keep the Controller code clean and focused on directing traffic.
Custom models must created in \brixy\App\Models\custom folder.
namespace brixy\App\Models\custom;
use CodeIgniter\Model;
class Product_Options_Model extends Model{
protected $db_conn;
function __construct(&$brixy_data = []){
$this->db_conn = make_db_conn(SHOP_CONNECTION_ID);
}
function add_option(){
//code
}
function edit_option($id){
//code
}
}
You can access models within your controller classes by define model path (use ...) and creating a new instance.
namespace brixy\App\Controllers\custom;
use CodeIgniter\Controller;
use brixy\App\Models\custom\Your_Custom_Model as Your_Custom_Model;
class Product_Options extends Controller{
private $Your_Custom_Model;
function __construct(){
helper(['common']);
$this->Your_Custom_Model = new Your_Custom_Model();
}
function your_method(){
//code
$this->Your_Custom_Model->your_func();
}
}
If you plan to use other models, they must be added and instantiated for them.
namespace brixy\App\Controllers\custom;
use CodeIgniter\Controller;
use brixy\App\Models\custom\Your_Custom_Model as Your_Custom_Model;
use brixy\App\Models\custom\Your_Custom_Model2 as Your_Custom_Model2;
use brixy\App\Models\custom\Your_Custom_Model3 as Your_Custom_Model3;
class Product_Options extends Controller{
private $Your_Custom_Model;
function __construct(){
helper(['common']);
$this->Your_Custom_Model = new Your_Custom_Model();
$this->Your_Custom_Model2 = new Your_Custom_Model2();
$this->Your_Custom_Model3 = new Your_Custom_Model3();
}
function your_method(){
//code
$this->Your_Custom_Model->your_func(); //access method your_func from Your_Custom_Model model
$this->Your_Custom_Model2->your_func();
$this->Your_Custom_Model3->your_func();
}
}