I am new to CI4, I have used CI2 & CI3 for the better part of 10 years. I am trying to create a model that will have a bunch of common database queries stored in functions. I would like to create a main model that will extend Model and then I will be able to extend it from a bunch of other models. I am hoping that someone can help me understand what is the best way to create commonly used functions. I don't know if maybe I should be doing this from Common.php.
The schedule controller - Schedule.php
PHP Code:
<?php
namespace App\Controllers\Main;
use App\Controllers\BaseController;
use App\Models\Schedule_Model;
class Schedule extends BaseController
{
// constructor
public function __construct() {
// load the schedule model
$this->sch_model = new Schedule_Model();
}
// the league details
public function view() {
// include scores
$this->data['season'] = $this->sch_model->test();
// add the css
$this->data['css'][] = 'schedule/index';
// render the view
return view('schedule/view', $this->data);
}
}
The Schedule Model - Schedule_Model.php
PHP Code:
<?php
namespace App\Models;
use CodeIgniter\Model;
class Schedule_Model extends OSM_Model
{
// get the current season
public function test() {
$this->current_season();
}
}
The Common Function Model - OSM_Model.php
PHP Code:
<?php
namespace App\Models;
use CodeIgniter\Model;
class OSM_Model extends Model
{
// get the current season
public function current_season() {
// do we have the current season already set
$sql = $this->db
->table('calendar_days as cd')
->select('cd.season_id, e.alt_name, e.name')
->join('smf_osm_events as e', 'cd.season_id = e.event_id', 'left')
->where('cd.date_date', date('Y-m-d'))->get();
foreach ($sql->getResult() as $r)
$data = $r;
// set the current season in a session
if (isset($data))
{
return [
'id' => $data->season_id,
'alt' => $data->alt_name,
'name' => $data->name
];
}
}
}
Thanks for the help.