Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9243743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:52:27+00:00 2026-06-18T08:52:27+00:00

Is it common and\or wise to write a generic insert function for a parent

  • 0

Is it common and\or wise to write a generic insert function for a parent model that child models will inherit from? I am working with CodeIgniter\PHP on a very simple CRUD app but this question is really language\framework independent.
Inheriting a read function that reads(selects) one value works very well and feels right:
Parent Model

public function read($columnSelect,$columnCondition,$value) {
    $query = "SELECT $columnSelect FROM $tableName WHERE $columConditionn=?";
    $res = $this->db->query($query,$columnName);
    if ($res->result()->num_rows()>0)
        return $res->result();
    else 
        return false;

this can allow you to retrieve user id’s , emails , check for existence of certain columns etc, definitely something I feel will be useful in most or all my models so it’s a good idea to write that function in the parent model. All the read functions that are more complicated will obviously be specific to each model (e.g queries that read more than one value etc)
Now what about insert? I can’t think of any generic insert besides writing functions to insert one value, two values etc or writing an ‘ugly’ function to will get an array of parameters and build the insert query according to the array, e.g:

        public function genericInsert($columnList,$valueList) {

              $query = " INSERT INTO $tableName(
                        //now code that parses columnList and adds ','
                       //and then code that adds VALUES(..) in the same manner

This definitely doesn’t feel like a “pattern” to me and feels wrong.
Am I overlooking something? What do you guys do for your inserts, is it specific to each model or do you try to figure a way to inherit from a parent model? And also, if you will, what are functions do you find yourself putting in your parent models? (updates,deletes etc)?

This is what my parent model looks like so far:

class MY_Model extends CI_Model {
    public $tableName;

    public function __construct($tableName) {
        parent::__construct();
        $this->tableName = $tableName;

    }

    public function read($columnSelect,$columnCondition,$value) {
        $query = "SELECT $columnSelect FROM $tableName WHERE $columConditionn=?";
        $res = $this->db->query($query,$columnName);
        if ($res->result()->num_rows()>0)
            return $res->result();
        else 
            return false;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:52:28+00:00Added an answer on June 18, 2026 at 8:52 am

    Hate answering my own question but this is what I was looking for
    http://thephpcode.com/blog/codeigniter/a-smart-codeigniter-model

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A common question that comes up from time to time in the world of
The Common Lisp HyperSpec says in the funcall entry that (funcall function arg1 arg2
I want to make sure that my web application is performance wise and will
Common scenario: I have a library that uses other libraries. For example, a math
A common problem is that for validation you need to run the same code
I am pretty sure that python scripts will work in all three, but I
A problem I was working on recently got me to wishing that I could
Common problem which I know the SQL gurus out there will be able to
A common component is a library or some other piece of code that is
I'm currently working on a multi-stage recipe for Capistrano that would, ideally, after deploy,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.