代码点火器模型

Getting an error HTTP 500 when calling

$this->load->model('cart/products', 'Products');

My folder structure is Applications/Models/cart/products.php

Not sure if possible to create subfolders in the Models folder in Code Igniter?

It should be $this->load->model('cart/Products_model', 'Products');

1) CI supports folders under Models too, so its not an issue.

2) Make sure class name in your model is also "products". File name and class name must be same.

3) It can also be issue with code inside your model. To debug it, just remove all the code from your model and just leave the class definition and check if it works.

You can try this, it works for me.

$this->load->model('cart/products');

i.e try removing the second parameter. I'm not sure this is the reason why you are getting the error but it doesnt hurt to try the simpler approach when you are in fix.

And then you can just use the 'products' model directly in your code. like so

$this->products->doSomething();

Hope it helps :)

yes you can

First load the model

$this->load->model('cart/products');

and then call the method inside the model like so

$result = $this->products->get_product($id);

here is my example that I just made in my wamp (should work under linux too)


my_controller.php

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

class my_controller extends CI_Controller {


    public function products()
    {
        $this->load->model("folder/my_model");

        $result = $this->my_model->get_product(9571);

        print_r($result);

    }

}

/* End of file my_controller.php */
/* Location: ./application/controllers/my_controller.php */ 

then created a folder in models like so

model-under-folder-code-igniter

and then here is the model my_model.php code

my_model.php

<?php
class my_model extends CI_Model
{

        public function get_product($id)
        {
            $q_str = "SELECT * from products WHERE products.id = ".$id;
            $q = $this->db->query($q_str);
            return $q->result();            
        }

}


?>

Cheers,

I think you should make the model file start with a capital letter:

products.php to Products.php

... and then use the below statement:

$this->load->model('cart/products');