Welcome Guest, Not a member yet? Register   Sign In
Load model in helper
#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


Messages In This Thread
Load model in helper - by 4usol - 12-27-2024, 05:41 AM
RE: Load model in helper - by captain-sensible - 12-27-2024, 06:13 AM
RE: Load model in helper - by 4usol - 12-27-2024, 09:53 AM
RE: Load model in helper - by captain-sensible - 12-28-2024, 04:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB