0

I cant seem to load application/model/Event.php model class then access a method from it.
Instead CI loads application/core/App_loader.php and tries to look for the method there.

Can anyone lend a hand on how to fix this please?

In: application/config/config.php

//$config['subclass_prefix'] = 'MY_';
$config['subclass_prefix'] = 'App_';

Event.php

class Event extends CI_Model {

  private $db_main;

  function __construct() {
    parent::__construct();
    $this->db_main = $this->load->database('main', TRUE);
  }

   function get($arr = array()) {
    // ! Trying to access this method ...
   }
}

From controller I am trying to load a model class called Event (verified that function index() gets called): application/controller/home.php

class Home extends App_Controller {

  private $event;

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

  function index() {
    $this->method1();
  }

  function method1() {
     $eventArr = $this->event->get(); // << Cant access method
  }

Message: Call to undefined method App_Loader::get()

inside application/core/App_loader.php:

class App_Loader extends CI_Loader {
  function __construct() {
    parent::__construct();
  }

  function aa(){}
  function bb(){}
  function cc(){}
}
6
  • use $this->load->model('event'); instead of $this->load->model('Event'); Commented Sep 5, 2018 at 14:17
  • that gives me Message: Cannot access private property Home::$event Filename: C:\xampp\htdocs\myproject\system\core\Loader.php Commented Sep 5, 2018 at 14:27
  • no need to create a variable $event, you can just load a model like $this->load->model('Event'); which makes available to use model methods directly using variable $this->event->get(); REFER LINK: codeigniter.com/userguide3/general/models.html#loading-a-model Commented Sep 5, 2018 at 14:36
  • when I comment out //private $event; on top and load model $this->load->model('Event'); then try to access method $this->event->get(); I get an error Message: Undefined property: Home::$event Commented Sep 5, 2018 at 14:48
  • 1
    try changing $this->load->model('Event'); to $this->load->model('event'); because CI documentation too shows, while accessing model name they are using model name in lower case. Commented Sep 5, 2018 at 14:55

1 Answer 1

1

Reference taken from https://www.codeigniter.com/userguide3/general/models.html#loading-a-model

class Event extends CI_Model {

    private $db_main;

    function __construct() {
        parent::__construct();
        $this->db_main = $this->load->database('main', TRUE);
    }

    function get($arr = array()) {
        // ! Trying to access this method ...
    }
}

class Home extends App_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('event');
    }

    function index() {
        $this->method1();
    }

    function method1() {
        $eventArr = $this->event->get();
    }
}
Sign up to request clarification or add additional context in comments.

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.