Met El Idrissi
Met El Idrissi

Reputation: 179

Load a model in Codeigniter 3.0.3

I have a project whith this structure in controllers folder:

And in my models folder:

  1. Place.php (the name inside is class Place extends ActiveRecord\Model)
  2. User.php
  3. ...

Inside my controller places.php if I want to load a model I have to do just that:

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

And after that I have to call my method like that:

$this->place->get_all_places();

It was work in my localhost but not in my server I check my php version in the server and it's 5.6.

How I can fix that?

That's my model file Place.php

    class Place extends ActiveRecord\Model
    {
        static $table_name = 'places';
        static $has_many = array(
        );


        public function get_all_places()
        { 
            return true;
        }
}

That's my controller file places.php

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

    class Places extends MY_Controller {

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

            if($this->user){
                $access = TRUE;

            }else{
                redirect('login');
            }

        }   
        function index()
        {
            $this->content_view = 'places/all';
        }

        function get_places(){

            $this->theme_view = '';

            $this->load->model('Place');
            $this->place->get_all_places();
        }
}

And the error it was that:

Unable to locate the model you have specified: place

Upvotes: 0

Views: 1335

Answers (2)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

I think every thing is right.But the problem is due to this line..

class Place extends ActiveRecord\Model

If you define a new model which extends the CI_Model.And then name of new model can not like as ActiveRecord\Model.Name may be as ActiveRecordModel.SO one solution for you can be..replace above line as below if you define the new model named as ActiveRecordModel in application/core folder.

class Place extends ActiveRecordModel

Another solution can be if you have not define any new model.then just replace above line as

 class Place extends CI_Model

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Your model file name should be

Places_model.php

and inside model

class Places_Model extends CI_Model # Changed 
{
    static $table_name = 'places';
    static $has_many = array();


    public function get_all_places()
    { 
        return true;
    }

}

Models in Codeigniter

Upvotes: 3

Related Questions