CodeIgniter can't call function from model in controller

心已入冬 提交于 2019-12-24 07:02:42

问题


I am fairly new to Codeigniter and am trying to call in a function from my model but I cannot get it to work. Can anyone see what I am doing wrong here?

Controller (farm.php):

<?php defined('BASEPATH') OR exit('No direct script access allowed');

    class Farm extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('harvest_first');
        }

        function harvest()
        {

            echo $this->harvest_first->harvest();
        }   
    }

Model (harvest_first.php):

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

    class Harvest_first extends CI_Model
    {

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

        public function harvest(){
            return "THE FUNCTION";
        }
    }
?>

I am trying to echo "THE FUNCTION", but no matter what I do I cannot get it to work as expected.

Thanks, Simon


回答1:


Try this

class Harvest_first extends CI_Model

change to :

class Harvest_first_model extends CI_Model

and in controller call like this:

 $this->load->model('harvest_first_model');

and

 $this->harvest_first_model->harvest();



回答2:


Check here

  • There is no need to add "_model" to model that depends on you
  • just Load the Model and that its use autoload.php and add model there it is a good pratices


来源:https://stackoverflow.com/questions/19180853/codeigniter-cant-call-function-from-model-in-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!