Welcome Guest, Not a member yet? Register   Sign In
Using common functions in multiple models
#11

[eluser]veliscorin[/eluser]
[quote author="tonanbarbarian" date="1202472502"]should be fine but if you post your actual code i can confirm what you are doing wrong[/quote]
system/application/libraries/MY_Model.php
Code:
class MY_Model extends Model {

    function MY_Model()
    {
    parent::Model();
    }

    function test()
    {
    return 'i want to see this line';
    }
    
}
system/application/models/user_model.php
Code:
class User_model extends Model{

    function User_model()
    {
        parent::Model();
    }
}
in my sample controller. i just load user_model and call $this->user_model->test(); and nothing appears.
#12

[eluser]Craig A Rodway[/eluser]
[quote author="veliscorin" date="1202422712"]I was thinking of doing it such a way that I shall have something like this:
Code:
some_generic_name->get_single_row($table_name, $id);
So that in my different controllers, I could use something like this:
Code:
$user = $this->some_generic_name->get_single_row('tbl_users', '1');
//or
$book = $this->some_generic_name->get_single_row('tbl_books', '1');
[/quote]

Yes, this is possible and is one way of achieving what you want. It's just a matter of coding your generic model to handle the correct parameters.

For example, I used this function in one of my apps to get data and supports pagination via per_page and start_at parameters.

Code:
function Get($table, $pk = NULL, $pk_id = NULL, $orderby = 'name asc', $per_page = NULL, $start_at = NULL){
    $this->db->select('*');
    $this->db->from($table);
    
    if($pk_id != NULL){
        // Getting only ONE row
        $this->db->where($pk, $pk_id);
        $this->db->limit('1');
        $query = $this->db->get();
        if( $query->num_rows() == 1 ){
            // One row, match!
            return $query->row();        
        } else {
            // None
            return false;
        }
    } else {
        // Get all
        if( $per_page != NULL){
            if( $start_at != NULL){
                $this->db->limit($per_page, $start_at);
            } else {
                $this->db->limit($per_page);
            }
        }
        $this->db->order_by($orderby);
        $query = $this->db->get();
        if( $query->num_rows() > 0 ){
            // Got some rows, return as assoc array
            return $query->result();
        } else {
            // No rows
            return false;
        }
    }
}
#13

[eluser]veliscorin[/eluser]
Hi Craig Rodway,
I understand how your function works and how it can fit into my situation. However, my concern now is to piece it all up together, and not so much on how the functions work. Could you show me where you placed this function in, how to use the functions in my controllers, and also how you coded your extended model class?
#14

[eluser]tonanbarbarian[/eluser]
your User_model should extend MY_Model not Model if you want to take advantage of the extra functions added to MY_Model
i suggest you read the link i provided above again




Theme © iAndrew 2016 - Forum software by © MyBB