开发者

codeigniter model not loading into controller, but no errors

开发者 https://www.devze.com 2023-02-07 18:12 出处:网络
I am having trouble with a models in Codeigniter. I setup a model called Usermodel in /models/usermodel.php. and when I attempt to load it is stops the scrip and anything after it.

I am having trouble with a models in Codeigniter. I setup a model called Usermodel in /models/usermodel.php. and when I attempt to load it is stops the scrip and anything after it.

When I purposely entered a wrong name in the ->load->model area CI throws up an error, saying it can't be found (like expected) When everything is correct nothing is loaded.

I for the life of me can't see what is going wrong. Any idea? Thanks, Tim

开发者_C百科

User model

class Usermodel {

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

    function displayUser()
    {
        echo "test";
    }
}

And the class is

class Page extends Controller {

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

    function index()
    {

        echo "<h1>Test</h1>";
        $this->load->model('usermodel');
        $this->usermodel->displayUser();
    echo "<p>Model loaded</p>";

    }

}


Depending on your version of CI, you need to extend the base Model, right now you are just making a new blank Class called Usermodel

example

class Usermodel extends CI_Model {   //<-- Note "extends CI_model"

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

NOTE

This syntax is for Codeigniter 2.0

Documentation


You missed to extend the class to 'Model' CI class:

class Usermodel extends Model {

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

    function displayUser()
    {
        echo "test";
    }
}


You need to load in the __construct first.

Note: the path isn't a url, it is a directory path.

public function __construct()
{
    parent::__construct();
    $this->load->model('path/to/your/usermodel');
}

Then in you index function is the same than you did.

function index()
{
    echo "<h1>Test</h1>";
    $this->load->model('usermodel');
    $this->usermodel->displayUser();
    echo "<p>Model loaded</p>";
}


I think this is a naming convention issue for models. If you are using codeigniter 3.x then see the documentation.

  • class Model_student extends CI_Model
  • save model as Model_student, Then load it as
  • $this->load->model(Model_student);

http://www.codeigniter.com/user_guide/general/models.html

I hope this will help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消