Welcome Guest, Not a member yet? Register   Sign In
Universal Save & Edit methods?
#1

(This post was last modified: 03-12-2016, 06:50 PM by solidcodes.)

Would anyone like to share the codes of these methods?

DRY....

thanks in advanced.
No SEO spam
Reply
#2

A lot of good stuff in this one.

https://github.com/jamierumbelow/codeigniter-base-model
Reply
#3

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

https://github.com/jamierumbelow/codeigniter-base-model
Reply
#4

@dmyers

+1 for sharing.

thanks.
No SEO spam
Reply
#5

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
No SEO spam
Reply
#6

(This post was last modified: 03-12-2016, 06:30 PM by solidcodes.)

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
No SEO spam
Reply
#7

(This post was last modified: 03-12-2016, 10:22 PM by ivantcholakov. Edit Reason: a typo )

@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/u...s-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-cop...-eloquent/

- Something similar I've seen in Bonfire: https://github.com/ci-bonfire/Bonfire/bl..._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. :-)
Reply
#8

@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.
No SEO spam
Reply
#9

(This post was last modified: 03-13-2016, 04:51 AM by Narf.)

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.
Reply
#10

Wow, -1 / 250 posts Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB