• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does MVC work for a page relating to several tables?

#1
[eluser]Unknown[/eluser]
Hello,

I'm looking for recommendations on how to organise code in a CodeIgniter-friendly manner for a Web page that displays information regarding several related tables all on a single page.

Simplifying the application somewhat, let's say I have three tables: schools, classes, and pupils. Every pupil belongs to a class, and every class belongs to a school. I already have a database table for each, a model for each, a controller for each, and several views for each. Most pages in the application are obviously either school-related, class-related, or pupil-related, so my intuition has informed me on where to put all the code. However, there is one page that relates to all at the same time. It is meant to be a single-page summary showing the user what information they are missing.

Pseudocode:

Code:
Check all school-related fields are non-empty; generate warnings if not
Get set of related classes
Check there is at least one related class; generate a warning if not
For each related class {
Check all class-related fields are non-empty; generate warnings if not
Get set of related pupils
Check there is at least one related pupil; generate a warning if not
For each related pupil {
  Check all pupil-related fields are non-empty; generate warnings if not
}
}

For each check that fails, I want a warning to be generated. The end goal is for the whole set of warnings to be displayed on a single summary page.

Since the page generally relates to a school, I have started with a function check() in the school controller. This could use the school model to get the values of the school-related fields, check that they are non-empty, and put together an array of warning strings for those that are not. I could then pass this array to a school-related view, and display the warnings to the user.

But what about the classes and pupils? At this point, I get stuck.

I have been doing the bulk of my data-checking and warning-generation in controllers. Following the structure of the data, I would expect that I should have analogous check() functions in the class and pupil controllers. Within the function check() in the school controller, I would repeatedly call the function check() in the class controller, and pass the return values from that function back to the function check() in the school controller. And within the function check() in the class controller, I would repeatedly call the function check() in the pupil controller, and pass the return values from that function back to the function check() in the class controller.

But if I understand correctly, CodeIgniter is not set up to load one controller inside another, and it would in any case be considered bad practice. Is my understanding correct? If so, what would be a good way of achieving my aim?

I could put all the check() functions into the respective models, but then I'd have to do something like passing the arrays of warning messages between models, which would be out of keeping with how the rest of the application works, and doesn't seem right.

Any advice would be appreciated!

Oliver

#2
[eluser]Unknown[/eluser]
I see that my post has had 50 views but no replies...

Was it too awkward a question? Did I put people off by making it too long? Or did I post it in the wrong place?

If this assuages any anxiety, I should make clear that although I posted this in "Code!", I'm not asking anyone to write the code for me. I just need advice about how to organise my own code. Sorry if that wasn't clear!

#3
[eluser]WanWizard[/eluser]
I read everything, but that doesn't imply I am all-knowing. Would you prefer 50 "I don't know" answers?

In standard CI, controllers can not call other controllers. For that, you would need an HMVC solution. But in this case, I don't see why you should.

But in this case I don't see why you should need this, the logic to retrieve the data should be in the model, either in one of the three, or in all three separate (which would require more controller code).


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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