Welcome Guest, Not a member yet? Register   Sign In
number of Model, question
#1

[eluser]imamiscool[/eluser]
In CI, if I want to make Model class,

should I make one Model for every table on my database
or
I make one model for each controller ( ie. each page have one corresponding model )

example:

contoller:
-Home
-User
-Order
-Register

database<my_site_db>:
-users (user_id, user_name)
-orders (order_id, stuff_id, order_total)
-users_orders (user_id, order_id)


should I make models:
this: UserModel, OrderModel, UserOrderModel
or
this one: HomeModel, UserModel, OrderModel, RegisterModel



I am stuck on creating function on Model which is JOIN several tables
Where should I put thefunction, ( which Model )


pardon my english
#2

[eluser]ztinger[/eluser]
Use as your convinience

you don't have to create a model for each controller.

You can even have one huge model with all the queries.

UserModel, OrderModel seems logical.
#3

[eluser]imamiscool[/eluser]
thank for super fast answer.

time to coding....
#4

[eluser]Luci3n[/eluser]
Also if your creating multiple models I also create a model with the basic functionality already for each new model I extend the base model class.

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

/**
* Generic Database helper class
*
* @package default
* @author    Clark Jones
**/
class Base_model extends Model {

    var    $table_name;    // Table name to use
    var $class_name;    // Class name for logging errors
    
    /**
     * Base_model Initialization
     *
     * @return void
     * @author Clark Jones
     **/
    function Base_model()
    {
        parent::Model();
    }
    
    /**
     * Generic read function
     *
     * @param mixed
     * @param int
     * @param int
     * @param mixed
     * @return void
     * @author Clark Jones
     **/
    function read($ar_filters = null, $limit = 10, $offset = 0, $ar_sort = null)
    {
        // Apply filters
        if ($ar_filters != null)
        {
            if (is_array($filters))
            {
                foreach ($filters as $field => $value)
                {
                    $this->db->where($field, $value);
                }
            }
        }
        
        if ($limit > 0)
        {
            $this->db->limit($limit, $offset);
        }
        
        if ($ar_sort != null)
        {
            if (is_array($sort))
            {
                foreach ($sort as $field => $order) {
                    $this->db->orderby($field, $order);
                }
            }
        }
        return $this->db->get($this->_tablename);
    }
    
    /**
     * Generic create function
     *
     * @param    array
     * @param    string
     * @return    mixed
     * @author    Clark Jones
     **/
    function create($ar_data)
    {
        $message = '';
        
        if ($ar_data == null)
        {
            $message = 'Incorrect data type $ar_data: null';
        }
        else
        {
            if (!is_array($ar_data))
            {
                $message = 'Incorrect data type $ar_data: not an array';
            }
            else
            {
                if ($this->db->insert($this->table_name, $ar_data) == TRUE)
                {
                    return $this->db->insert_id();
                }
                $message = 'Data failed to be inserted into database: '.$this->db->last_query());
            }
        }
        log_message('error', $this->class_name.'::create - '.$message);
        return null;
    }
    
    /**
     * Generic update function
     *
     * @param    array
     * @param    array
     * @return    bool
     * @author    Clark Jones
     **/
    function update($ar_filters, $data)
    {
        $message = '';
        
        if ($ar_filters == null)
        {
            $message = 'Incorrect data type $ar_filters: null';
        }
        else
        {
            if (is_array($ar_filters) == false)
            {
                $message = 'Incorrect data type $ar_filters: not an array';
            }
            else
            {
                foreach ($ar_filters as $field => $value)
                {
                    $this->db->where($field, $value);
                }
                // Perform Update
                if  ($this->db->update($this->table_name, $data))
                {
                    return true;
                }
                else
                {
                    $message = 'Data failed to be updated in database: '.$this->db->last_query();
                }
            }
        }
        log_message('error', $this->class_name.'::update - '.$message);
        return false;
    }

    /**
     * Generic delete function
     *
     * @param    array
     * @return    bool
     * @author    Clark Jones
     **/
    function delete($ar_filters)
    {
        $message = '';
        
        if ($ar_filters == null)
        {
            $message = $this->class_name.'Incorrect data type $ar_filters: null';
        }
        else
        {
            if (is_array($ar_filters) == false)
            {
                $message = 'Incorrect data type $ar_filters: not an array';
            }
            else
            {
                if  ($this->db->delete($this->table_name, $ar_filters))
                {
                    return true;
                }
                else
                {
                    $message = 'Data failed to be deleted: '.$this->db->last_query();
                }
            }
        }        
        log_message('error', $this->class_name.'::delete - '.$message);
        return false;
    }

}
?&gt;

to extend the class something like this

Code:
require_once(APPPATH.'models/base_model.php');

class Accounts_model extends Base_Model {
    
    function Accounts_model()
    {
        parent::Base_Model();
        $this->load->database();
        $this->table_name = 'accounts';
        $this->class_name = 'Accounts_model';
    }
}
#5

[eluser]ztinger[/eluser]
good post Luci3n.

you could post your model template in the wiki, as for tips and tricks.
#6

[eluser]Developer13[/eluser]
When I start a project, I usually start the model as one file (mdl_db) and see where it takes me. I see no need to complicate things with multiple models if you don't need them.

Instead of having a separate model for each table or controller with common functions such as get, save, delete, etc etc, I might have one model that has getUsers, saveUser, getItems, saveItem, etc.

It all really depends on the application for me.
#7

[eluser]David.M[/eluser]
Is there a way to implement the base_model with joins?




Theme © iAndrew 2016 - Forum software by © MyBB