CodeIgniter Forums

Full Version: Array in model weirdness
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Shodan[/eluser]
Hi guys,

Please excuse me, I'm relatively new to CI, although I've made a couple of site's based on it, I'm still fairly "green".

I've come across a weird situation involving an array in a model. I'm try to make a standard "shopping cart" (called a "quick quote" in this instance) comprising of one "Quick_quote_cart_model" object containing many "Quick_quote_item_model" objects.

The items are stored in an array called $cart_items which is a property of the "Quick_quote_cart_model" object.

Here's the relevant code snippets, first the "Quick_quote_cart_model":
Code:
class Quick_quote_cart_model extends model
{
    var $cart_items = Array();
    
    /**
    * Quick_quote_cart_model::Quick_quote_cart_model()
    *
    * @access     public
    * @since     Version 2.0
    */
    function Quick_quote_cart_model()
    {
        parent::Model();
    }
// ------------------------------------------------------------------------
    /**
    * Quick_quote_cart_model::add_item()
    *
    * @access     public
    * @since     Version 2.0
    * @param     $_item object A quick_quote_item_model object.
    */
    function Add_cart_item($_item)
    {
        $item_id = $this->Total_items()+1;
        
    [line 36]$this->cart_items[$item_id]         = new Quick_quote_item_model();
        $this->cart_items[$item_id]->product_id        = $_item->product_id;
        $this->cart_items[$item_id]->product_name    = $_item->product_name;
        $this->cart_items[$item_id]->product_code    = $_item->product_code;
        $this->cart_items[$item_id]->product_thumb    = $_item->product_thumb;
        $this->cart_items[$item_id]->item_engraved    = $_item->item_engraved;
        $this->cart_items[$item_id]->qty            = $_item->qty;
        $this->cart_items[$item_id]->logo_colours    = $_item->logo_colours;
        $this->cart_items[$item_id]->size        = $_item->size;        
    }
....
....
....

"Quick_quote_item_model":
Code:
class Quick_quote_item_model extends model
{
    var $product_id         = "";
    var $product_name        = "";
    var $product_code        = "";
    var $product_thumb        = "";
    var $item_engraved        = "";
    var $qty                = 1;
    var $logo_colours        = "";
    var $size                = "";
// ------------------------------------------------------------------------
    /**
    * Quick_quote_item_model::Quick_quote_item_model()
    *
    * @access     public
    * @since     Version 2.0
    * @param     $_product_id     integeger
    * @param     $_product_name     string
    * @param     $_product_code     string
    * @param     $_product_thumb string
    * @param     $_item_engraved string
    * @param     $_qty             integer
    * @param     $_logo_colours     string
    * @param     $_size             string                            
    */
    function Quick_quote_item_model()
    {
        parent::Model();
    }
// ------------------------------------------------------------------------
    /**
    * Quick_quote_item_model::Quick_quote_item_model()
    *
    * @access     public
    * @since     Version 2.0
    * @param     $_product_id     integeger
    * @param     $_product_name     string
    * @param     $_product_code     string
    * @param     $_product_thumb string
    * @param     $_item_engraved string
    * @param     $_qty             integer
    * @param     $_logo_colours     string
    * @param     $_size             string                            
    */
    function Init(
                $_product_id
                ,$_product_name
                ,$_product_code
                ,$_product_thumb
                ,$_item_engraved
                ,$_qty
                ,$_logo_colours
                ,$_size                                        
                )
    {
        $this->product_id       = $_product_id;
        $this->product_name    = $_product_name;
        $this->product_code    = $_product_code;
        $this->product_thumb    = $_product_thumb;
        $this->item_engraved    = $_item_engraved;
        $this->qty        = 1;
        $this->logo_colours    = $_qty;
        $this->size        = $_size;
    }
// ------------------------------------------------------------------------
}

and here's the controller code that adds the item:
Code:
class Quick_quote extends Controller {

  var $per_page = 20; // number of items to list per page
  var $_quick_quote_cart = false;

    function Quick_quote()
    {
        parent::Controller();
                ...
                ...
        $this->load->model("Quick_quote_cart_model");
        $this->load->model("Quick_quote_item_model");        
                ...
                ...
        ...
        // initialise a new cart object
        $this->_quick_quote_cart    = new Quick_quote_cart_model();
        ...
        ...
        ...

        function Add()
        {
        ...
        ...
        ...
        $cart_item = new Quick_quote_item_model();
    $cart_item->Init(
            $prod_details['prod_id']
            ,$prod_details['prod_name']
            ,$prod_details['prod_code']
            ,$prod_details['pic_thumb']
            ,$this->input->post("engraved")
            ,$this->input->post("qty")
            ,$this->input->post("logo_colours")
            ,$this->input->post("item_size")
            );
    $this->_quick_quote_cart->Add_cart_item($cart_item);

continued in next post...

El Forum

[eluser]Shodan[/eluser]
...continued from previous post

This results in the following error message:

“Fatal error: Cannot use object of type Quick_quote_cart_model as array in C:\xampp\htdocs\tm_v2\application\models\quick_quote_cart_model.php on line 36”

I’m baffled. This code is based on a non-CI system that works perfectly, so the theory as far as I can see is sound.

Even more bizarrely, if you remove the “$this->” the assignment lines of the “Quick_quote_cart_model->Add_cart_item()” function, it’ll get past the initial error (causing more problems further down the line).

I’m assuming this is some sort of scope issue but I really have no idea what’s going on.

Any ideas?

ANY help would be appreciated at this stage.

Thanks in advance,
Terry

El Forum

[eluser]Shodan[/eluser]
Looks like I was being an "Eedjit"...

One of the lines I removed from the code snippets retrieved the cart object from the session. Instead of then getting the array from the cart object and using that for further processing, I was using the object itself, causing some sort of circular assign itself to itself schenanigans.

Anyway, fixed now!