[eluser]D_Williams[/eluser]
My goal is to make a "batch letter sender" utility script that interacts with our main account database to schedule letters to be sent. All my script needs to do to schedule the letter is insert a row into a table so that's cake.
The basic flow for my script will be this:
- User fills out my form
- Form is validated and cleaned up
- Display a list of all accounts that will be affected and have them confirm
Now, I'm allowing the user to choose three constraints here for which accounts have letters sent on them:
1. Which client the account belongs to
2. What day the account was imported into our system
3. What status the account is set to
Now without explaining fully about our system, these three things can obviously just be thought of as parts of the where clause of my initial select. Now so far I have the view done and it works fine, no need to post the code here. What I'm having trouble with is how to create my models (which will be reused for other pages under this same project eventually, the letter sending is just a small function of what it will do eventually), and how to keep SQL out of my controller. I know that CI is lax on enforcing strict MVC rules but I'd like to stick to good design wherever possible.
For starters, here's my controller as it is now (rather barebones):
Code:
function batch_mail_sender()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('client', 'Client', 'trim|required');
$this->form_validation->set_rules('type', 'Letter Type', 'trim|required');
$this->form_validation->set_rules('calendar_dateplaced_year', 'Year', 'trim|required');
$this->form_validation->set_rules('calendar_dateplaced_month', 'Month', 'trim|required');
$this->form_validation->set_rules('calendar_dateplaced_day', 'Day', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
// Form has not been run or there are errors
$this->load->library('form_elements');
$data['content_view'] = 'utilities/batch_mail_form';
$data['page_title'] = 'Batch Mail Sender';
$data['javascript'][] = 'calendar.js';
$data['css'][] = 'calendar.css';
$this->load->view('templates/popup', $data);
}
else
{
// Form validation success, process away
}
}
All that's there so far is the code to set up my view and validate the form. It's time to start filling in the logic down in the "else" statement at the bottom and I'm a bit stumped on how to proceed.
Now, if I were coding with standard procedural PHP here my obvious next step would be to make a query where the client name is "this", the status name is "that", and the date imported is "whatever" and proceed from there with the results. Without making some model and model function highly specific to this particular task (like GetAccountsWhereClientIsXAndStatusIsYAndDateImportedIsZ(x,y,x) which seems a bit ridiculous), how can I query the database with those constraints?
I'm not sure if I've designed myself into a hole here? Is this a situation in which it would be OK to query the database in my controller?