Universal Save & Edit methods? |
03-12-2016, 10:19 PM
(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. :-) |
Messages In This Thread |
Universal Save & Edit methods? - by solidcodes - 03-12-2016, 12:06 AM
RE: Universal Save & Edit methods? - by dmyers - 03-12-2016, 08:55 AM
RE: Universal Save & Edit methods? - by ivantcholakov - 03-12-2016, 09:24 AM
RE: Universal Save & Edit methods? - by solidcodes - 03-12-2016, 03:51 PM
RE: Universal Save & Edit methods? - by solidcodes - 03-12-2016, 06:01 PM
RE: Universal Save & Edit methods? - by solidcodes - 03-12-2016, 06:24 PM
RE: Universal Save & Edit methods? - by ivantcholakov - 03-12-2016, 10:19 PM
RE: Universal Save & Edit methods? - by solidcodes - 03-12-2016, 10:52 PM
RE: Universal Save & Edit methods? - by Narf - 03-13-2016, 04:50 AM
RE: Universal Save & Edit methods? - by ardhie1032 - 03-13-2016, 06:08 AM
RE: Universal Save & Edit methods? - by solidcodes - 03-13-2016, 07:03 AM
|