Welcome Guest, Not a member yet? Register   Sign In
would like some help getting a model to work
#1

[eluser]Flying Fish[/eluser]
Thus far I've been just to db queries in my controller, but I'd like to start using models, as that seems to be a better practice.

Here's a rough model I'm using
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*

    The Order model is used by the admin controller to display orders

*/


class Orders extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function all_orders()
    {
        $query = $this->db->get('nbt_orders');
        return $query->result();
    }

    function new_orders()
    {
        $query = $this->db->get_where('nbt_orders', array('status' => 'ordered'));
        return $query->result();
    }

    function old_orders()
    {
        
        $query = $this->db->get('nbt_orders');
        return $query->result();
    }

}






/* End of file order_model.php */
/* Location: ./system/application/models/order_model.php */


Here's my controller
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*

    The Admin class is where we can view and manage orders.

*/


class Admin extends Controller {

    function __construct()
    {
        parent::Controller();
        
        // Customize the tags that wrap arround the form validation error messages
        //$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        // Restrict access to Admins only
        if($this->authentication->check_admin() === TRUE)
        {
            return TRUE;
        } else
        {
            show_error('404');
        }
        
        $this->load->model('Orders');
    }
    
    function index()
    {
            
        $data['title'] = "Admin :: NBT Supplies";
        $data['heading'] = "Admin";
        //$data['query_new_orders'] = $this->db->get_where('nbt_orders', array('status' => 'ordered'));
        $data['query_new_orders'] = $this->Orders->new_orders();
        $data['query_orders'] = $this->db->get_where('nbt_orders', array('status' => 'printing', 'status' => 'shipping', 'status' => 'complete'));
                
        // Load this view by default
        $this->load->view('admin_index', $data);
    }
    
}


/* End of file admin.php */
/* Location: ./system/application/controllers/admin.php */

But I'm getting this error message.

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Admin::$Orders

Filename: controllers/admin.php

Line Number: 37

Fatal error: Call to a member function new_orders() on a non-object in /Applications/MAMP/htdocs/staging2/system/application/controllers/admin.php on line 37

Anyone know why this is happening? Thanks,
#2

[eluser]TheFuzzy0ne[/eluser]
Ensure the file name of your model is all lowercase.
#3

[eluser]Thorpe Obazee[/eluser]
Fuzzy, man you're all around the block... :p
#4

[eluser]TheFuzzy0ne[/eluser]
Not anymore. 4:50AM = way past bed time. G'night!
#5

[eluser]Thorpe Obazee[/eluser]
haha.. yeah g'night!
#6

[eluser]jedd[/eluser]
Btw, all_orders() and old_orders() are going to return identical sets of data.
#7

[eluser]Flying Fish[/eluser]
Thanks guys. looks like changing the first letter to uppercase did the trick.

I should probably include an if statement in my model to make sure some data is returned, right?

Like the if (num_rows > 0) thingy?

@jedd
thanks for looking out for me, I'll be putting some different where filters in old_orders, but I wanted to make sure my model was working first :-)
#8

[eluser]zutis[/eluser]
This is how I would do it ... this way you should find it easier to add more tables (like order_products etc) in the future


Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*

    The Order model is used by the admin controller to display orders

*/


class Orders extends Model {

    function __construct()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function all_orders()
    {
        $this->db->from('nbt_orders');
        $query = $this->db->get();
        return $query->result();
    }

    
    // Can pass an array or a single staus as a string
    function at_status($status='')
    {
        if(is_array($status))
        {
            $this->db->where_in('status', $status);
        }
        elseif($status !== '')
        {
            $this->db->where('status', $status);
        }
        
        return $this->all_orders();
    }

    function old_orders()
    {
        return $this->all_orders();
    }

}






/* End of file order_model.php */
/* Location: ./system/application/models/order_model.php */




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

/*

    The Admin class is where we can view and manage orders.

*/


class Admin extends Controller {

    function __construct()
    {
        parent::Controller();
        
        // Customize the tags that wrap arround the form validation error messages
        //$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        // Restrict access to Admins only
        if($this->authentication->check_admin() === TRUE)
        {
            return TRUE;
        } else
        {
            show_error('404');
        }
        
        $this->load->model('Orders');
    }
    
    function index()
    {
            
        $data['title'] = "Admin :: NBT Supplies";
        $data['heading'] = "Admin";
        //$data['query_new_orders'] = $this->db->get_where('nbt_orders', array('status' => 'ordered'));
        $data['query_orders'] = $this->orders->at_status(array('status' => 'printing', 'status' => 'shipping', 'status' => 'complete'));
                
        // Load this view by default
        $this->load->view('admin_index', $data);
    }
    
}
#9

[eluser]Flying Fish[/eluser]
@zutis
looks handy, thanks

that would keep the number of functions in my model more manageable too




Theme © iAndrew 2016 - Forum software by © MyBB