Codeigniter Model Optimization I have created this model for Codeigniter. It's working fine, but I need to know if it can be more optimized.

class Basic_Function_Model extends CI_Model 
{

    var $msg_invalid_array;
    var $msg_no_table;

    function __construct()
    {
        parent::__construct();
        $this-&gt;msg_invalid_array    =   "Data must be provided in the form of array";
        $this-&gt;msg_no_table         =   "Table does not exist in database";
        $this-&gt;load-&gt;database();
    }

    public function insertInDatabase($table, $insertData, $mode = "single")
    {
        if( !is_array($insertData) ) {
            return $this-&gt;msg_invalid_array;
        }

        if( !$this-&gt;validateTable($table) ) {
            return $this-&gt;msg_no_table; 
        }

        if( $mode == "batch" ) {
            return $this-&gt;db-&gt;insert_batch($table, $insertData);
        } else {
            $this-&gt;db-&gt;insert($table, $insertData);
            return $this-&gt;db-&gt;insert_id();
        }
    }

    public function updateInDatabase($table, $updateData, $conditionData)
    {
        if( !is_array($updateData) || !is_array($conditionData) ) {
            return $this-&gt;msg_invalid_array;
        }

        if( !$this-&gt;validateTable($table) ) {
            return $this-&gt;msg_no_table; 
        }

        if( $this-&gt;db-&gt;update($table, $updateData, $conditionData) )
            return $this-&gt;db-&gt;affected_rows();
        else
            return false;
    }

    public function deleteFromDatabase($table, $conditionData)
    {
        if( !is_array($conditionData) ) {
            return $this-&gt;msg_invalid_data;
        }

        if( !$this-&gt;validateTable($table) ) {
            return $this-&gt;msg_no_table; 
        }

        return $this-&gt;db-&gt;delete($table, $conditionData);
    }

    public function insertOnDuplicateKeyUpdate($table, $tableData)
    {
        if( !is_array($tableData) ) {
            return $this-&gt;msg_invalid_data;
        }

        if( !$this-&gt;validateTable($table) ) {
            return $this-&gt;msg_no_table; 
        }

        foreach( $tableData as $column =&gt; $value )
        {
            $columnNames[] = $column;
            $insertValues[] = "'".$value."'";
            $updateValues[] = $column." = '".$value."'";
        }
        $this-&gt;db-&gt;query("insert into $table(".implode(', ', $columnNames).") values(".implode(', ', $insertValues).") on duplicate key update ".implode(', ', $updateValues));
        return $this-&gt;db-&gt;insert_id();
    }


    private function validateTable($tableName)
    {
        $result = $this-&gt;db-&gt;list_tables();

        foreach( $result as $row ) {
            if( $row == $tableName )    return true;
        }
        return false;
    }
}
First of all, thanks for sharing your code, this is the best way to improve yourself!

The M in MVC

A Model in any MVC framework should provide an easy way to access a specific type of data. It allows you to stop worrying about database details and only access your specific type of data in a meaningful way. For a StackOverflow-like website, this means that your Controller can relax! Your controller doesn't want to know about INSERT INTO Questions(id, title, message, author) VALUES (9624, "Codeigniter Model Optimization", "...", "Shayan"). He just wants to do $questions-&gt;add("Codeigniter Model Optimization", "...", "Shayan") and know that all details (including security) are going to be handled by the Model.

The model should be in charge of ensuring everything goes well with the database, but that's not what you're doing here. You're creating a Leaky Abstraction, and this doesn't help you. Try coming up with a model that reduces the work the controller needs to do.

A few comments on the code itself.


You're repeating yourself a lot at the start of each function. One fix would be to put this in a function:
    $checks = $this->check($table, $conditionData);
    if ($checks !== FALSE) return $checks;
But the real fix is to remove those. $this-&gt;db-&gt;query() already removes FALSE when there's an issue. You should use it to check if there's an issue, and then decide what to do then. The handling of issues is what makes abstractions successful. Remember than "It's easier to ask for forgiveness than it is to get permission".
Please use query bindings (at then end of the page) which will avoid SQL injections. They're using PHP's prepared statements, which you should learn about if you want to write secure applications.