• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Models won't load

#1
None of my models will load. No matter which one. I've tried different names, but I always get: "An Error Was Encountered

Unable to locate the model you have specified.” I'm at a loss.

/models/menu_model.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Menu_model extends CI_Model {
    
    function 
__construct() {
            
parent::__construct();
    }
    
    function 
get_active_albums() {
        
$query $this->db->get_where('albums', array('is_active' 1));
        
        return 
$query->result();
    }


/controllers/welcome.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
    
    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('menu');
    }
    
    public function 
index() {
        
$menu $this->create_menu();
        
$this->load->view('header');
        
$this->load->view('welcome_message');
        
$this->load->view('footer');
    }
    
    private function 
create_menu() {
        
$data['query'] = $this->menu->get_active_albums();
        echo 
$data;
    }

Reply

#2
You named your model Menu_model so you need to load it as Menu_model not just Menu.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Just wondering, why are you including this line at the top of your model and controller?
PHP Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

Thanks!
Reply

#4
Because he doesn't want that anyone who types domain.com/controllers/welcome.php in his browser will load the actual file behind that url.
It's a general CI convention if you look at the source code on the CI github repo.
Reply

#5
(12-07-2014, 04:48 AM)bw.balazs Wrote: Because he doesn't want that anyone who types domain.com/controllers/welcome.php in his browser will load the actual file behind that url.
It's a general CI convention if you look at the source code on the CI github repo.

Thanks! I didn't know.
Reply

#6
(12-06-2014, 06:26 PM)InsiteFX Wrote: You named your model Menu_model so you need to load it as Menu_model not just Menu.

I've tried that. When I do it that way, I just get a blank screen. I don't even get the error message.
Reply

#7
Also, when I check my developer tools, I keep getting "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"
Reply

#8
Are you checking your server error log file?
The location of that file depends on the server used.
Apache (linux) is usually in /var/log/apache2.error.log

Also, change the model name (menu -> Menu_model) in ALL uses in your Welcome controller, I found two uses in the code posted above. The name is case sensitive on most OS's.
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply

#9
Sad 
Okay, let me lay this out again. I'm developing on OpenShift by Red Hat. I've setup application just as the tutorial/walkthrough says to. I can't get models to load, and I keep getting a 500 error. I've checked the log files.

When I load the webpage I get this in the logs:
Code:
[08/Dec/2014:20:13:52 -0500] "GET / HTTP/1.1" 500 - "http://2scsb-aaronfenker.rhcloud.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25"

If the filename of my model includes "_model, “ the page gives the CI error message, but it still throws the 500 error. If I don't include "_model" in the filename, then the page doesn't load at all, but it still throws the 500 error.

When I do a git push I get the following errors:
Code:
[08/Dec/2014:20:11:04 -0500] "HEAD / HTTP/1.1" 500 - "-" "Ruby"
[08/Dec/2014:20:11:04 -0500] "HEAD / HTTP/1.1" 500 - "-" "Ruby"
[08/Dec/2014:20:11:04 -0500] "HEAD / HTTP/1.1" 500 - "-" "Ruby"
[08/Dec/2014:20:11:04 -0500] "HEAD / HTTP/1.1" 500 - "-" "Ruby"

I deleted my original app, and started fresh just in case I goofed on something in pushes. (Git is new to me too.)

Here's my code again.

controllers/welcome.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Welcome extends CI_Controller {

    
/**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *         http://example.com/index.php/welcome
     *    - or -  
     *         http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    
public function index()
    {
        
$this->load->model('Nav_menu');
        
$this->load->view('header');
        
$this->load->view('welcome_message');
        
$this->load->view('footer');
    }
    private function 
create_menu() {
        
$data['query'] = $this->Nav_menu->get_active_albums();
     
   echo $data;
 
  }


models/nav_menu.php
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Nav_menu extends CI_Model {
    function 
__construct() {
     
       parent::__construct();
    }
    
    function 
get_active_albums() {
        
$query $this->db->get_where('albums', array('is_active' 1));
        
        return 
$query->result();
    }


Is there a setting that I'm missing somewhere?
Reply

#10
I guess you are using CI 2 so...

Try this in you controller - i just changed the model name to nav_m


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Welcome extends CI_Controller {
 
  
    public 
function index()
 
   {
 
       $this->load->model('nav_m');
 
       $this->load->view('header');
 
       $this->load->view('welcome_message');
 
       $this->load->view('footer');
 
   }
 
   private function create_menu() {
 
       $data['query'] = $this->nav_m->get_active_albums();
 
       echo $data;
 
  }



And this in your model but don't forget to change the file name of the model in nav_m.php

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Nav_m extends CI_Model {
 
   function __construct() {
 
           parent::__construct();
 
   }
 
   
    function get_active_albums
() {
 
       $query $this->db->get_where('albums', array('is_active' 1));
 
       
        return $query
->result();
 
   }

Romanian CodeIgniter Team :: Translations :: Comunity :: Developers
http://www.codeigniter.com.ro
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.