Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 2 - Inserting Database Records with a Code...
MISCELLANEOUS CODE

Inserting Database Records with a CodeIgniter PHP Framework Model
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2009-09-02

    Table of Contents:
  • Inserting Database Records with a CodeIgniter PHP Framework Model
  • Review: fetching database rows with the generic model
  • Coding a method for inserting database records
  • The complete source code of the generic model class

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Inserting Database Records with a CodeIgniter PHP Framework Model - Review: fetching database rows with the generic model


    (Page 2 of 4 )

    It’s possible that you want to know how to implement a method within the generic model class shown in the previous article of this series that allows to insert new rows into the specified database table. But before I show you the signature of the method, I'd first like to list the complete source code of the model in its current state, so you can recall in a snap how it looked initially.

    That being said, here’s the complete signature of the aforementioned model class:

    abstract class MY_Model extends Model

    {

    protected $table = ''; // table associated to the model

    protected $fields = array(); // fields of table associated to the model

    protected $id = NULL; // value of the primary key of the table associated to the model

    protected $data = array(); // model input data

    protected $insertID = NULL; // insertion ID

    protected $numRows = NULL; // number of rows returned by SELECTS

    protected $validation = array(); // model validation rules

    protected $errors = array(); // model errors

     

    /**

    * Constructor

    *

    * @access protected

    */

    protected function __construct()

    {

    parent::Model();

    // get CI super object as a model property

    $this->ci =& get_instance();

    }

     

    /**

    * Sets associated table data for the model

     *

    * @author Alejandro Gervasio

    * @return void

    * @access public

    */

    public function setTableData($table = 'default')

    {

    if ($this->db->table_exists($table))

    {

    $this->table = $table;

    $this->fields = $this->db->field_names($this->table);

    }

    }

     

    /**

    * Sets value of primary key of the associated table for the model

     *

    * @author Alejandro Gervasio

    * @param integer

    * @return void

    * @access public

    */

    public function setID($id)

    {

    $this->id = is_integer($id) AND $id > 0 ? $id : 1;

    }

     

    /**

    * Gets value of primary key of the associated table for the model

     *

    * @author Alejandro Gervasio

    * @return integer

    * @access public

    */

    public function getID()

    {

    return $this->id;

    }

     

    /** Sets input data for the model

     *

    * @author Alejandro Gervasio

    * @param array

    * @return void

    * @access public

    */

    public function setData($data)

    {

    if ( is_array($data) AND count($data) > 0)

    {

    foreach ($data as $key => $value)

    {

    if (array_search($key, $this->fields) === FALSE)

    {

    unset($data[$key]);

    }

    }

    $this->data = $data;

    }

    }

     

    /**

    * Sets validation rules for model data

     *

    * @author Alejandro Gervasio

    * @param array

    * @return void

    * @access public

    */

    public function setValidation($validation)

    {

    if ( is_array($validation) AND count($validation) > 0)

    {

    foreach ($validation as $field => $rule)

    {

    if (array_search($field, $this->fields) === FALSE)

    {

    unset($validation[$key]);

    }

    }

    $this->validation = $validation;

    }

    }

     

    /**

    * Returns a result set with specified fields according to given conditions.

    *

    * @author Alejandro Gervasio

    * @return query result on success - Boolean FALSE on failure

    * @access public

    */

    public function fetch($fields = '*', $where = NULL, $order = 'id ASC', $limit = NULL, $offset = 0, $join = NULL)

    {

    if ($fields != '*')

    {

    $this->db->select($fields);

    }

    if ($this->id != NULL)

    {

    $this->db->where('id', $this->id);

    }

    elseif($where != NULL)

     {

    $this->db->where($where);

    }

    if ($order != 'id ASC')

    {

    $this->db->orderby($order);

    }

    if ($limit != NULL)

    {

    $this->db->limit($limit, $offset);

    }

    if( $join != NULL)

    {

    $this->db->join($join);

    }

    $query = $this->db->get($this->table);

    $this->numRows = $query->num_rows();

    if ($this->numRows > 0)

    {

    return ($this->numRows > 1 ) ? $query->result() : $query->row();

    }

    $this->errors[] ='No rows were returned by the query.';

    return FALSE;

    }

    }

    As you’ll probably recall, the above “AbstractModel” class includes enough functionality for setting the data and the validation rules that will be used by it when performing CRUD operations. Most importantly, however, it implements a core method called “fetch(),” which fetches rows from the specified database table in accordance with certain conditions.

    From the signature of this method, it’s clear to see that it acts as a simple wrapper for many of the methods of the native Active Record class provided by CodeIgniter, so again I recommend that you read the corresponding user guide if you’re not very familiar with it yet.

    Well, at this stage you hopefully recalled how the “AbstractModel” class looks now, so it’s time to continue enhancing its current functionality. As I expressed in the introduction, it’s necessary to give this class the ability to insert new rows into the associated database class, so in the next section I’m going to code another core method that will be charged with performing this crucial task.

    As usual, to see how this method will be defined, click on the link that appears below and keep reading.

    More Miscellaneous Code Articles
    More By Alejandros Gervasio

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap