-2

I am trying to load model in my view file but problem is model is not loading and given error Message: Undefined property: CI_Loader::$CoupanCatModel

Following is my code

Controller

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

class CoupanCategory extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('coupans/CoupanCatModel');
        //$this->load->model('ForeignData_model');
    }
    public function init(){
        $logoUrls = 'no-image.jpg';
        $webUrls = 'no-image.jpg';
        $mobileUrls = 'no-image.jpg';

        $this->load->library('upload');

        $config['upload_path'] = './assets/images/coupans/category/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size']      = 20000; 
        $config['max_width']     = 1024; 
        $config['max_height']    = 768;

        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('ccImage')){
            $error = array('error' => $this->upload->display_errors());     
        }
        else{
            $data = $this->upload->data();          
            $logoUrls = $data['file_name'];
        }

        $is_init = $this->CoupanCatModel->init($logoUrls);
        if ($is_init) {
            redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=success&success=New coupan category create','refresh');
        }
        else
        {
            redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=failed&error=something want wrong','refresh');
        }


    }

}

Model

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

class CoupanCatModel extends CI_Model {

    public function init($logoUrls)
    {

        new_id: 

        $sc_id = 'SC'.mt_rand(1000,9999);

        if ($this->check_id($sc_id)) 
            goto new_id;

        $sc_title = (isset($_POST['coupan_cat_name']) && $_POST['coupan_cat_name'] != '') ? trim($_POST['coupan_cat_name']):'';

    $sc_logo = $logoUrls;

    //  $sc_web_image = $webUrls;
        //$sc_mobile_image = $mobileUrls;

        $active_flag =1;

        $this->db->set('oc_cat_id',$sc_id);
        $this->db->set('oc_cate_name',$sc_title);
        //$this->db->set('sc_description',$sc_description);
    //  $this->db->set('sc_logo',$sc_logo);
        $this->db->set('oc_web_image',$sc_logo);
        //$this->db->set('sc_mobile_image',$sc_mobile_image);

        $this->db->set('active_flag',$active_flag);

        $this->db->insert('tbl_oc_category');

        return true;
    }   



    public function get_data($oc_cat_id = false)
    {


        if ($oc_cat_id != false) {
            $this->db->where('oc_cat_id', $sc_id);
            $query = $this->db->get('tbl_oc_category');
            return $query->row_array();
        }
            $query = $this->db->get('tbl_oc_category');
            return $query->result_array();
    }


}

View i just load model like

<?php

$this->load->model('coupans/CoupanCatModel');                               
$scData = $this->CoupanCatModel->get_data();

echo '<pre>ddsd';
print_r($records);
die();
?>

But problem is model is not loading and given error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$CoupanCatModel

Filename: coupans/coupan_category.php

Line Number: 139
Samir Sheikh
  • 2,283
  • 2
  • 19
  • 37

1 Answers1

1

For your own reference, you might want to read the Codeigniter Style Guide for filenames etc.

The code you have provided appears to be "dubious" in the fact there is no where in your code where you are loading the alleged "view".

So based upon what you have provided, I've come up with some Demo/Debug code.

Just note that your use of calling models inside views isn't quite correct in the scheme of MVC, nor is it entirely illegal.

So I've set this up on a Linux machine where filenames etc are case sensitive.

controllers/coupans/CoupanCategory.php

class CoupanCategory extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('coupans/CoupanCatModel');
    }

    public function index() {
        echo "I am the controller ".__CLASS__;
        $this->CoupanCatModel->init();

        $this->load->view('coupans/coupan_category');
    }
}

models/coupans/CoupanCatModel.php

class CoupanCatModel extends CI_Model{

    public function init($logoUrls = '')
    {
        echo "<br>I am the ".__METHOD__;
    }

    public function get_data($oc_cat_id = false)
    {
        return "<br>I am the ".__METHOD__;
    }
}

views/coupans/coupan_category.php

<?php

$this->load->model('coupans/CoupanCatModel');
$scData = $this->CoupanCatModel->get_data();

var_dump($scData);

And from out of all that you should get...

I am the controller CoupanCategory
I am the CoupanCatModel::init
string '<br>I am the CoupanCatModel::get_data' (length=37)

That should give you something to help solve your issue.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • @Wolfgang1983 - Why oh why did you go and change all of the case on my filenames??? While that style doesn't follow the Codeigniter Style guide, it does work as I had actually Created the Code and Fully tested it... If you want to point out the issue with filename naming, then please post it as a new answer and comments to suit. – TimBrownlaw Nov 29 '17 at 21:42
  • I that was was I was taught by the CI people. The showed me the example of the first letter upper case only for class and filename –  Nov 29 '17 at 21:46
  • @wolfgang1983 By the way - Views do not need to be 1st letter capitalised. – TimBrownlaw Nov 29 '17 at 21:47
  • @wolfgang1983 - Yes it is what you were taught... But it doesn't mean it wont work. It was interesting to prove that the naming conventions used by the OP do in fact work. It's a fine line to say - "You must do it this way or else bad things will happen". The fact the OP is loading and using models inside a view is "cringe worthy" but you can't say it won't work. Thats the thing with CI - good or bad, it will let you do things in a number of ways. – TimBrownlaw Nov 29 '17 at 21:51
  • Thanks for the info Tim I plus 1 for your answer any way –  Nov 29 '17 at 21:52
  • @wolfgang1983 - If you are going to change things - please don't go introducing typos... like class Coupancamodel :) – TimBrownlaw Nov 29 '17 at 21:54
  • I can roll it all back it if you want –  Nov 29 '17 at 21:56
  • @wolfgang1983 - That would be nice and I'll add a comment regarding the CI style guide for reference. – TimBrownlaw Nov 29 '17 at 21:57
  • Done rolled back –  Nov 29 '17 at 21:58
  • @wolfgang1983 - You should see if that code works for you as an exercise :) – TimBrownlaw Nov 29 '17 at 22:03