CodeIgniter Forums
Load model in helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Load model in helper (/showthread.php?tid=92233)



Load model in helper - 4usol - 12-27-2024

hi,

i need to know if i have to load a wanted model in every helper function again and again or is there a way to load it once and how i can access them.

I try in my helper
"
Code:
use CodeIgniter\CodeIgniter;
use App\Models\Model;
use App\Models\DepartmentModel;

$Departmentmodel = model('DepartmentModel');

if (! function_exists('hlp_error_redirect'))
{
    function hlp_department_get(int $id,array $fields=array())
    {
        //Load needed Models
        $Departmentmodel = model('DepartmentModel');
        $res=$Departmentmodel->departments_get_by_id($id,$fields);  
        //$res=$this->Departmentmodel->departments_get_by_id($id,$fields);  
"

Info: the departmentmodel extend the model

Access with $this failed = Using $this when not in object context

What is the best way to load the model and make it accessable in the helper functions?


RE: Load model in helper - captain-sensible - 12-27-2024

a start of one of my models is :

Code:
<?php

namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;


class BlogModel extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'blog';
    protected $primaryKey      = 'Id';
    protected $useAutoIncrement = true;
    protected $returnType      = 'array';
    protected $useSoftDeletes  = false;
    protected $protectFields    = true;
      protected $allowedFields = ['title','article','image','slug','date','font'];

...
}

to use the model in a controller :

Code:
$this->handle = new BlogModel();
//then to use any method in BlogModel() it will just be :

$result= $this->handle->getArticle($theSlug);
......................

$handle is a property of my class
getArticle() is a method of my Model class
$theSlug is a variable of model class ,for which the value i obtained using my controller , to access my database ,to get data and return it to the controller . getArticle() will itself use the value of $theSlug abd use it to query database and return values ,which will be data for a blog Article

no helpers , no messing , field are declared in properties model


if you follow the appropriate architecture and place your models in app/Models and use namespace, 
Code:
namespace App\Models;

Then when you instantiate with :
Code:
$this->handle = new BlogModel();

then class will be looked for, found and be instantiated. because we are extending "model" then various things are inbuilt


RE: Load model in helper - 4usol - 12-27-2024

@captain-sensible
Thank for your detailed code, it makes something clear for me.

But let me ask again: Is there any way to setup a global db-connection, to avoid, setup the db connection in each model again and again?

Also my main-question, is how i can load the model in the helper, look at my code, i dont understand why i cannot go in this way...


RE: Load model in helper - captain-sensible - 12-28-2024

@usol global connection ? To use a model class ,you have to instantiate it. When you instantiate it connects to default database; but its only one line to switch to another database set out in app/config/Database.php so why write more lines of code that I dont have to ?