CodeIgniter Forums
Universal Save & Edit methods? - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Best Practices (
+--- Thread: Universal Save & Edit methods? (/showthread.php?tid=64612)

Pages: 1 2

Universal Save & Edit methods? - solidcodes - 03-12-2016

Would anyone like to share the codes of these methods?


thanks in advanced.

RE: Universal Save & Edit methods? - dmyers - 03-12-2016

A lot of good stuff in this one.

RE: Universal Save & Edit methods? - ivantcholakov - 03-12-2016

A really good example how the testing mania could destroy a project. A subjective opinion, of course.

RE: Universal Save & Edit methods? - solidcodes - 03-12-2016


+1 for sharing.


RE: Universal Save & Edit methods? - solidcodes - 03-12-2016

off topic.

I can't believe NARF just give me -1 for this thread.

@Narf Just because you did not understand the topic, that doesn't mean a -1 rating is right???

I'm glad the two other guy above understood me.
Sad, NARF is not so smart.

-1 for NARF

RE: Universal Save & Edit methods? - solidcodes - 03-12-2016

Here is a good example of a universal save method.

function save($op=NULL,$where=array())
        $model_for="Product ";
        $return=(object)array('status'=>false,'data'=>'Error Occured');
        $this->create(); //Initializes a model for writing.
        if(isset($posted_data) && is_array($posted_data) && count($posted_data))
           foreach($this->input->post() as $field=>$value)
              $data[trim($field)]=(is_array($value) || is_object($value)) ? $value : trim($value);
          case 'add':
              $return->data=$model_for."added successfully";
          case 'edit':
              $return->data=$model_for."edited successfully";
          case 'del':
             $return->data=$model_for."removed successfully";
        return $return;    

The reason I'm asking because perhaps their codes are better than what I have here.
Disappointed to you Sad

RE: Universal Save & Edit methods? - ivantcholakov - 03-12-2016


It is not a good example, let me explain why.

1. You want "universal save", from your code I can see that you are making something as a CRUD system. The word "universal" is problematic here, as we know any abstraction leaks eventually. Let us say you are looking something that serves 80-90% of your possible cases.

2. Code that you provided tries to make an abstraction, but by using quite primitive means. It is verbose. Where it exists, within a base model or it is to be copy-pasted within all the relevant models? DRY?

3. You try to avoid annoying multiple assignments POST -> data_to_be_saved, by assuming that POST keys match the table field names. I use this trick too, but in your implementation I see two (for now) problems: nothing filters the auxiliary POST variables that are not to go within the database (hidden form fields, "Yes, I agree with the terms and conditions"); on updating nothing protects the primary key value.

4. I would propose to you to research for prepared code that is aimed to ease CRUD operations. These are the options I know:

- - It is a good start for reading. A very brief article could be found here: Unfortunately at the moment this project looks like "abandonware", at the moment I don't see activity since the year of 2014, this is before the release of CI3.

- - This is something that I (silently) support. It is a fork of Jamie Rumbelow's work, partly compatible, with extras, still works with CI2, and works with CI3 for sure.

- , here is an article:

- Something similar I've seen in Bonfire:

Maybe a full-ORM package would be a solution too, but this I leave for you to think about.

5. Your question requires in details a whole chapter of a book, please, don't be disappointed. :-)

RE: Universal Save & Edit methods? - solidcodes - 03-12-2016


thanks for trying to understand but the example codes above is not mine.
I used it sometimes before but it is from another programmer.

thanks for the suggestion.

I'm dissapointed because NARF rated me -1 without asking first for clarification.

RE: Universal Save & Edit methods? - Narf - 03-13-2016

Right ... let's now mention every time that Narf gave you a -1. Since you want to talk about it so badly, here's the deal:

Most, if not all of your posts on this forum are like this topic - vague at best, missing key details, always in horrible English and with a complete lack of effort on your part, often showing that you barely have a clue of what you're trying to do.

And I've only given you -1 after over 250 such posts.
If you opened one such thread on StackOverflow, within minutes you'd be downvoted into oblivion and the question would be closed. Suck it up and move on.

RE: Universal Save & Edit methods? - ardhie1032 - 03-13-2016

Wow, -1 / 250 posts Smile