Welcome Guest, Not a member yet? Register   Sign In
error with loading model
#1

[eluser]babyboss[/eluser]
Following is my display.php code under my model folder,
Code:
<?php
  class Display extends Model {

   /*create the array to pass to the views*/
       var $data = array();
   /*two other class variables*/
    var $base;
    var $status = '';

  /*the constructor function: this calls the 'model' parent class, loads
  other CI libraries and helpers it requires, and dynamically sets
  variables*/
    function Display()
    {
         parent::Model();
         $this->load->helper('form');
         $this->load->library('user_agent');
         $this->load->library('errors');
         $this->load->library('menu');
         $this->load->library('session');

  /*now set the standard parts of the array*/
         $this->data['css']  = $this->config->item('css');
         $this->data['base'] = $this->config->item('base_url');
         $this->base          = $this->config->item('base_url');
         $this->data['myrobots'] = '<meta name="robots"
                                    content="noindex,nofollow">';
  /*note that CI's session stuff doesn't automatically recall the extra
  variables you have added, so you have to look up the user's status in
  the ci_sessions table*/
          $sessionid = $this->session->userdata('session_id');
          $this->db->select('status');
          $this->db->where('session_id', $sessionid);
          $query = $this->db->get('ci_sessions');
            if ($query->num_rows() > 0)
                   {
                   $row = $query->row();
                  $this->status = $row->status;
                  }

       }
  /*function to assemble a standard page. Any controller can call this.
  Just supply as $mydata an array, of key/value pairs for the contents
  you want the view to display. Available variables in this view are:
  mytitle. menu, mytext, diagnostic
  */
      function mainpage($mydata)
            {
            $this->data['mytitle'] = 'Monitoring website';
            $this->data['diagnostic'] = $diagnostic;
            foreach($mydata as $key => $variable)
            {$this->data[$key] = $variable;}
  /*here's the menu class we looked at in Chapter 3*/
            $fred = new menu;
            $this->load->library('session');
            $mysess = $this->session->userdata('session_id');
            if(isset($this->status) && $this->status > 0)
                   {$this->data['menu']=
                                     $fred->show_menu($this->status);}
            $this->load->view('basic_view', $this->data);

           }

  }
  ?>


I load the above in myController.php using the following
Code:
<?php

class MyController extends Controller {

    function MyController()
    {
        parent::Controller();    

    }
    
    function index()
    {
    $data['mytitle'] = "A website monitoring tool";
    $data['mytext'] = "This website helps you to keep track of
                      the other websites you control.";
    $data['myrobots'] = '<meta name="robots" content="noindex, nofollow">';
    $data['mywebtitle'] = 'Web monitoring tool';
    $data['base'] = $this->config->item('base_url');
    $data['css'] = $this->config->item('css');
    $data['header'] = $this->load->view('header_view', '', TRUE);
    
  
    
  
        $this->load->model('display');
        $this->display->mainpage($data);
        
        
        
    }
}

I type the following in my browser address bar:
http://localhost/codeigniter2/index.php/...ller/index

and I get the following error message:

An Error Was Encountered

Unable to load the requested class: errors
#2

[eluser]Dam1an[/eluser]
Not sure if it's cauing the problem, but the filename of the controller should be all lower case (use underscores as word seperators) and then the class and constructor should only have the first character capitalized
#3

[eluser]jedd[/eluser]
Don't load views from your model.
#4

[eluser]Dam1an[/eluser]
I didn't even notice s/he was loading views in the model, just saw the casing being incorrect in the controller, so made a point of that and went back to work

Upon closer inspection, it appears to be a renderer like library, but it's a model :-S
Why not use a library for this?
#5

[eluser]babyboss[/eluser]
I am pretty new to codeigniter, the code snippet is from a book I have been reading. I am not sure how I correct the problem.
#6

[eluser]jedd[/eluser]
[quote author="babyboss" date="1245371901"]I am pretty new to codeigniter, the code snippet is from a book I have been reading. I am not sure how I correct the problem.[/quote]

If the quote is verbatim - if the author really did try to load a view ($this->load->view...) from a model as you've shown above - then you should probably stop reading that book.

The way to correct the problem in the immediate short term is to relocate your view loads into your controller. At least, I expect it'll resolve your problem. Certainly it'll make your life much easier.

Think of the controller as being, well, kind of like it was in control. And your model as being subservient to the controller. The model does nothing other than take and receive data (and do whatever fiddling is necessary to your database).

The controller talks to the model, and similarly it talks to (and controls) the view(s).
#7

[eluser]gtech[/eluser]
the problem you are having is that errors and menu are not standard CI libraries, CI cannot find the errors library and that is why you are seeing that error message.

have you created the libraries in the ..\system\application\libraries directory?

Code:
//this is the problem line
$this->load->library('errors');

* although loading a view in a model is a tad unauthodox it will still work (I just tested it). *
#8

[eluser]babyboss[/eluser]
Hello, gtech, that is impressive, how did you figure out it's that particular line cause the problem? thank you.




Theme © iAndrew 2016 - Forum software by © MyBB