• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using one controller/method to validate and send different types of data to model

#1
[eluser]skunkbad[/eluser]
I have used a config file to change form validation rules dynamically, but after validation succeeds, I'm wondering what my best option is for sending the different data sets to the model for insert or update. In a simple form validation example, the form validation is run, and if not FALSE, then set_value() can be applied to array members that are passed to the model for insert/update. What I want to do is dynamically change that array. For instance, if I was validating the post, and it had post keys a, b, and c, then route to model->insert( $insert_array, $type). If the post keys had d, e, and f, then I'd change the $type. What I'm wondering about is setting up the array from set_value()s. Do you think it is appropriate to send the array to the model and have it sort out everything, or would it be better to do it in the controller? There are many $types that need to be sorted out, so I'm just wondering if anyone has any advice. What I don't really want to do is have my controller grow by a few hundred lines trying to sort out all of these things, but then I don't really want it all in the model either!

#2
[eluser]toopay[/eluser]
The first thing in my mind : if i have many task like that, i'll create some helper or (even) library to handle that. Sorting or prepend data is controller job, not model.

#3
[eluser]skunkbad[/eluser]
[quote author="toopay" date="1303077746"]The first thing in my mind : if i have many task like that, i'll create some helper or (even) library to handle that. Sorting or prepend data is controller job, not model.[/quote]

This is exactly the decision I have to make. How to handle the multiple sets of data, and where to handle them. If I do them all separately, then there will be a lot of repetitive code.

#4
[eluser]toopay[/eluser]
Share your "multiple sets of data" (example) here, so we can get an idea of how it might work on a helper or library. What i think is, in your case, we can use some library, something like...
Code:
//In controller, we can get sorted types 'flag', stored in some variable.
$this->load->library('typesorter');
$sorted_type = $this->typesorter->sort($some_pre_sorted_type);
//....

#5
[eluser]skunkbad[/eluser]
I ended up coming up with a nice solution. I use a uri segment to designate which set of data I'm going to insert. That uri segment is part of the table name, name of validation rule set, name of view, etc.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.