0

My application have this folders stucture:

  • application

    • library

      • Model.php
      • Controller.php
      • View.php
    • models
      • Settings_Model.php
      • Home_Model.php
    • controllers
      • settings.php
      • home.php
    • views
      • settings
        • index.php
      • home
        • index.php
  • index.php
  • .htaccess

library/Model.php

 class Database extends PDO{
        public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS){
              parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);       
        }
  }

library/Controller

 class Controller{

      function __construct(){       
           $this->view = new View();
      }

      public function loadModel($name){
           $path = 'application/models/' . $name . '_model.php';

           if(file_exists($path)){
                require 'application/models/' . $name . '_model.php';
                $modelName = $name . '_Model';
                $this->model = new $modelName();            
           }
      }
 }

library/View

 class View{

      function __construct(){       
           $this->view = new View();
      }

      public function render($name){
           require 'application/views/' . $name . '.php';
      }
 }

In Models folder I created this model called Settings_Model.php : models/Settings_Model.php

 class Settings_Model extends Model{
    public function __construct(){parent::__construct();}

    function settings(){
       $stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
       $stmt->execute();
       $stmt->setFetchMode(PDO::FETCH_OBJ);
       return $stmt->fetch();
    }       
 }

In Controllers folder I have this Controller called settings.php : controllers/settings.php

class Settings extends Controller {

    function __construct(){parent::__construct();}

    public function index(){    
        $this->view->settings = $this->model->settings();   
        $this->view->render('settings/index');      
    }   
}

And In Views folder I created a folder called settings and inside this folder I created an index.php file: settings/index

and this is how to call the settings method inside settings/index:

$this->settings->fieldName;

So now when we go to Home view, I did the same procedure: - create a Home_Model class and inside this model I created the same settings method:

 class Home_Model extends Model{
    public function __construct(){parent::__construct();}

    function settings(){
       $stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
       $stmt->execute();
       $stmt->setFetchMode(PDO::FETCH_OBJ);
       return $stmt->fetch();
    }       
 }

and then goes to the controllers folder and create Home controller:

class Home extends Controller {

    function __construct(){parent::__construct();}

    public function index(){    
        $this->view->settings = $this->model->settings();   
        $this->view->render('home/index');      
    }   
}

and then call the settings method inside home/index:

$this->settings->fieldName;

What Im asking is how to avoid not to create the same method settings again and again each time we need to call that method in another view file???

NB: Im still a newbie.

2 Answers 2

1

Try like this in your controller

class Settings extends Controller {
   function __construct(){
     parent::__construct();
     $this->load->model('Settings_Model');
   }
   public function index(){    
     $this->view->settings = $this->Settings_Model->settings();   
     $this->view->render('settings/index');      
   }   
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply, but My question is how not recreate the same settings method in every single model file? and how to call the same method in different controllers?
you can call model in your controller(controller.php) in contructor. then you can use its functions
In your case, you can call model by in constructor of controller.php $CI =& get_instance(); $CI->load->model('Settings_Model');
Sorry but I didn't know what $CI =& get_instance(); means? and how to use it. Kindly give me a clear exemple so that a newbie like me can understand well :)
you can check the codeigniter userguide. It has example and explanation also. ellislab.com/codeigniter/user-guide/general/…
0

if you want to access the same settings() function in all models then you should create it in Base model.php.

class Model{


  function settings(){
       $stmt = $this->db->prepare("SELECT * FROM `mv_settings`");
       $stmt->execute();
       $stmt->setFetchMode(PDO::FETCH_OBJ);
       return $stmt->fetch();
    }

  }

then your settings model need to be like

 class Settings_Model extends Model{
    public function __construct(){parent::__construct();}

 }

and your Home_model

 class Home_Model extends Model{
    public function __construct(){parent::__construct();}

 }

Now you can access settings() in all your models extending Model, no need to create it again and again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.