Model CRUD Generator for CodeIgniter

CodeIgniter is a very easy PHP framework, and most people will agree that it has a very detailed and easy-to-understand document which provide developers so much conveniences.

Somehow, there are some limitations on CodeIgniter as well. We know that for other frameworks such as Yii, a built-in tool (such as Gii) will help you to generate the Model CRUD codes for all those tables in your database, which will make your life much easier. For CodeIgniter, there is no "Official" tools provided. When you are developing a large scale projects which might associate with a big database, it might be a nightmare to create all those CRUD functions for your models.

So here is a simple "Model CRUD Generator" code for CodeIgniter. Please take a try on this code, any feedback will be appreciated!

 \$item['" . $row_column['column_name'] . "'],\n\t\t\t";
                }
                else{
                    $str_create .= "'" . $row_column['column_name'] . "' => \$item['" . $row_column['column_name'] . "']\n\t\t\t";
                }
            }
            if($row_column["column_key"] != "PRI")
            {
                if($index!=$total_num){
                    $str_update .= "'" . $row_column['column_name'] . "' => \$item['" . $row_column['column_name'] . "'],\n\t\t\t";
                }
                else{
                    $str_update .= "'" . $row_column['column_name'] . "' => \$item['" . $row_column['column_name'] . "']\n\t\t\t";
                }
            }
            else
            {
                $str_read .= "\$this->db->select('*');\n\t\t";
                $str_read .= "\$this->db->from('" . strtolower($tb_name) . "');\n\t\t";
                $str_read .= "\$this->db->where('" . $row_column['column_name'] . "', \$id);\n\t\t";
                $str_delete .= "\$this->db->where('" . $row_column['column_name'] . "', \$id);\n\t\t";
                $str_update_col = $row_column['column_name'];
            }
            $index++;
        }

        $str_create .= " ) \n\n\t\t";
        $str_create .= "\$this->db->insert('" . strtolower($tb_name) . "', \$data);\n\t";
        $str_create .= "}\n\n\t";
        $str_update .= " ) \n\n\t\t";
        $str_update .= "\$this->db->where('" . $str_update_col . "', \$id);\n\t\t";
        $str_update .= "\$this->db->update('" . strtolower($tb_name) . "', \$data);\n\t";
        $str_update .= "}\n\n\t";
        $str_delete .= "\$this->db->delete('" . strtolower($tb_name) . "');\n\t";
        $str_delete .= "}\n";
        $str_read .= "\$query = \$this->db->get();\n\n\t\t";
        $str_read .= "if(\$query->num_rows()<1){\n\t\t\t";
        $str_read .= "return null;\n\t\t";
        $str_read .= "}\n\t\t";
        $str_read .= "else{\n\t\t\t";
        $str_read .= "return \$query->row();\n\t\t";
        $str_read .= "}\n\t";
        $str_read .= "}\n\n\t";
        $str_readAll .= "\$this->db->select('*');\n\t\t";
        $str_readAll .= "\$this->db->from('" . strtolower($tb_name) . "');\n\t\t";
        $str_readAll .= "\$query = \$this->db->get();\n\n\t\t";
        $str_readAll .= "if(\$query->num_rows()<1){\n\t\t\t";
        $str_readAll .= "return null;\n\t\t";
        $str_readAll .= "}\n\t\t";
        $str_readAll .= "else{\n\t\t\t";
        $str_readAll .= "return \$query->result_array();\n\t\t";
        $str_readAll .= "}\n\t";
        $str_readAll .= "}\n\n\t";

        fwrite($ftable, $str_create);
        fwrite($ftable, $str_read);
        fwrite($ftable, $str_readAll);
        fwrite($ftable, $str_update);
        fwrite($ftable, $str_delete);
        fwrite($ftable, "}");
        fclose($ftable);
        /*Generating Model Code End*/
    }
?>

GitHub Link
Google+

Leave a Comment

You must be logged in to post a comment.