Welcome Guest, Not a member yet? Register   Sign In
Load model in helper
#1

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?
Reply
#2

(This post was last modified: 12-27-2024, 06:26 AM by captain-sensible. Edit Reason: addition )

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
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#3

(This post was last modified: 12-27-2024, 09:55 AM by 4usol.)

@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...
Reply
#4

@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 ?
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB