Database Update Question |
[eluser]ehalber[/eluser]
Currently, I'm using a hidden form field to capture the id of the database record I want to update. Next I save the post vars in my controller and send them to my model. Is this the standard way to perform this operation? Are there alternative ways, better ways? Code from controller Code: function update_news() Code from model Code: function update_entry($id, $update_info) Thanks
[eluser]Derek Allard[/eluser]
Not a very helpful response from me, but that's nearly exactly how I do it - I don't see a more elegant way off hand.
[eluser]LuckyFella73[/eluser]
Another approach would be to set the id as the last URL segment of your form/action-parameter and get the id in the method by using the url_segment function. But like I said, it's just an other approach and not meant to be more elegant than using the hidden form field.
[eluser]eggshape[/eluser]
I took an alternative approach. I saved the id of the target table to the cookie (and sessions table) and used an inherited method to update the table. Note, this involves modifying the Sessions class to accept non-default fields and the Model superclass, so that any derived classes inherit the method. In each model's constructor, I retrieve the id and place it in a protected property. So when I call $this->$model->update(), there is no need to pass arguments and retrieve hidden-data.
[eluser]ehalber[/eluser]
@Derek, thanks for the quick reply. I also wanted to say thanks for the great tutorial you have on your site, it really helped me to see how to integrate Code Igniter with JavaScript, my first love. I just started a blog of my own, codelicker.com. My second entry was a short note on available code igniter tutorials. While the list is short, I think it definitely covers the basics for those needing a quick introduction. Collecting these tutorials and others like them into one place on the code igniter site might be a great resource for newbies. Well, I just finished driftstudio.org, it's a very simple site showcasing work and news by five collaborating members. One is my brother, Geoff Halber, who is a senior designer at Dwell magazine. The tagline reads: Drift is a cross-disciplinary design studio working in the fields of architecture and graphic design. While there's no work yet, I'm hoping they'll have some time to get their contributions together soon. Anyway, I'm excited about getting to know code igniter better and exploring some of its other features in future projects.
[eluser]ehalber[/eluser]
@luckyfella73 I originally was trying to do that, but since I was using a submit button, there is no uri segment. Am I missing something?
[eluser]Negligence[/eluser]
You're doing it the typical way. Just make sure you're performing some validation to see if you're working with a valid record (ID) before you use it in a query.
[eluser]LuckyFella73[/eluser]
What I meant was: In your view file you have the form tag: Code: <?= form_open('controller_update/method_update/'.$id_to_update);?> By submitting the form via your button, the form send the post data to your controller - for example: www.yoursite.com/controller_update/method_update/12 In your method you get the value using: Code: $id_to_update = $this->uri->segment(3)
[eluser]LuckyFella73[/eluser]
@ehalber your are welcome! Regarding to your last question: If Derek uses to take the hidden-form-field approach I would tend to do it his way But maybe it's just a matter of style. Your question is interesting for me too so it would be great if other members would post some advantages and disadvantages for both mehtods. |
Welcome Guest, Not a member yet? Register Sign In |