Welcome Guest, Not a member yet? Register   Sign In
Tutorial - Use new table
#3

You create a MY_Model

PHP Code:
// ./application/core/MY_Model.php

class MY_Model extends CI_Model
{
    
/**
     * --------------------------------------------------------------------
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

 
       public $tableName     // Holds the database table name.

    
public $limit 20;        // Default database record limit.


    // --------------------------------------------------------------------

    /**
     * __Construct()
     *
     * Constructor    PHP 5+    NOTE: Not needed if not setting values!
     *
     */
 
        public function __construct()
 
        {
 
           parent::__construct();
 
        }

    
/**
     * -----------------------------------------------------------------------
     * Return Object's:
     * result() = $row->title;
     * row()    = $row->title;
     *
     * Return Array's:
     * result_array() = $row['title'];
     * row_array()    = $row['title'];
     * -----------------------------------------------------------------------
     */

    // -----------------------------------------------------------------------

    /**
     * setTableName()
     *
     * @param  string $tableName
     */
    
public function setTableName($tableName '')
    {
        
$this->tableName $tableName;
    }

}

// ./application/models/Your_model.php

class Your_model extends MY_Model
{

    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
    
public function __construct()
    {
        
parent::__construct();

           
$this->setTableName('tableName');

        
log_message('debug'"Name Model Class Initialized");
    }

    
/**
    * getPost ()
    * -------------------------------------------------------------------
    *
        * EXAMPLE:
        *
    * @param  string $id
    * @return array
    */
    
public function getPost($id '')
    {
        
$data = array();

        
$this->db->where('id'$id);
        
$this->db->limit(1);

        
$query $this->db->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            
$data $query->row_array();
        }

            
$query->free_result();

            return 
$data;
    }



Place all of your general code in the MY_Model and  extend it to your models.

You can now use setTableName() to change the name of the table.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
Tutorial - Use new table - by JeffreyB - 11-18-2017, 09:34 AM
RE: Tutorial - Use new table - by Wouter60 - 11-18-2017, 12:26 PM
RE: Tutorial - Use new table - by JeffreyB - 11-18-2017, 08:56 PM
RE: Tutorial - Use new table - by JeffreyB - 11-18-2017, 09:00 PM
RE: Tutorial - Use new table - by InsiteFX - 11-18-2017, 12:39 PM
RE: Tutorial - Use new table - by Wouter60 - 11-19-2017, 12:52 AM
RE: Tutorial - Use new table - by InsiteFX - 11-19-2017, 05:06 AM
RE: Tutorial - Use new table - by JeffreyB - 11-20-2017, 12:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB