CodeIgniter Forums
Universal Save & Edit methods? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- 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?

DRY....

thanks in advanced.


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

A lot of good stuff in this one.

https://github.com/jamierumbelow/codeigniter-base-model


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.

https://github.com/jamierumbelow/codeigniter-base-model


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

@dmyers

+1 for sharing.

thanks.


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.

Code:
function save($op=NULL,$where=array())
   {
        $model_for="Product ";
        $return=(object)array('status'=>false,'data'=>'Error Occured');
        $this->create(); //Initializes a model for writing.
        $posted_data=$this->input->post();
        $data=array();
        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);
           }
        }
        switch($op)
        {
          case 'add':
            $this->data=$data;
            if($this->insert())
            {
              $return->status=true;
              $return->data=$model_for."added successfully";
            }
          break;
          case 'edit':
            $this->data=$data;  
            $this->where=$where;
            if($this->update())
            {
              $return->status=true;
              $return->data=$model_for."edited successfully";
            }
          break;
          case 'del':
             $this->primaryKey='id';
             $this->id=$where['id'];
             $this->remove();
             $return->status=true;
             $return->data=$model_for."removed successfully";
          
          break;
          default:
          break;
        }
        return $return;    
    }

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


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

@solidcodes

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:

- https://github.com/jamierumbelow/codeigniter-base-model - It is a good start for reading. A very brief article could be found here: http://www.codebyjeff.com/blog/2012/01/using-jamie-rumbelows-my_model 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.

- https://github.com/ivantcholakov/codeigniter-base-model - 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.

- https://github.com/avenirer/CodeIgniter-MY_Model , here is an article: http://avenir.ro/revisiting-my_model-copying-jamie-rumbelow-looking-eloquent/

- Something similar I've seen in Bonfire: https://github.com/ci-bonfire/Bonfire/blob/0.8.3/bonfire/core/BF_Model.php

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

@ivantcholakov

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