Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 3 - 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 - Coding a method for inserting database records


    (Page 3 of 4 )

    Definitely, one of the principal features that any decent model class must have is the capability to insert and update rows in a specified database table. With that in mind, in the next few lines I’m going to add another method to the “AbstractModel” class, which will be tasked with performing insertions and updates against its associated table.

    Here’s how this brand new method looks:

    /** Saves model data into associated table (validation rules are applied to input data)

    *

    *

    * @author Alejandro Gervasio

    * @return integer on success - Boolean FALSE on failure

    * @access public

    */

     

    public function save()

    {

    if ($this->data == NULL)

    {

    $this->errors[] = 'Error saving row.';

    return FALSE;

    }

    // validate input data

    if( !$this->validate())

    {

    return FALSE;

    }

    // Insert new row if ID was not set in the model

    if ($this->id == NULL)

    {

    $this->db->insert($this->table, $this->data);

    $this->insertID = $this->db->insert_id();

    return $this->insertID;

    }

    // Otherwise update existing row

    else

    {

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

    return $this->db->affected_rows();

    }

    }

    As you can see, the logic implemented by the previous “save()” method is fairly easy to follow. In this case, the method will insert a new record into the specified database only if the $this->id property has been previously set. Otherwise, it’ll perform an update operation using the aforementioned property.

    Finally, it’s valid to mention that if no data is supplied for inserting or updating a record in the database, then the corresponding error message will be stored in the $this->errors array and the method will return a FALSE value. Not too difficult to grasp, right?

    So far, so good. Now that you hopefully understood how the previous “save()” method does its thing, it’s time to see how the generic model looks after incorporating this new method. So, in the last section of this tutorial I’m going to list for you the complete source code of the model, so can see more clearly how it is now structured.

    Please click on the link below and read the following segment.

    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 11 - Follow our Sitemap