Composite controllers?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Composite controllers?

Post by superdezign »

I'm not exactly sure how to approach this, so I'll try to explain my situation.

I'm building the back-end for an MMORPG database website that I'm developing. One example of what I'm trying to do is Equipment Sets. Each set has certain equipment that, when a character wears more than one piece, provide bonuses. Here are the related tables:

Code: Select all

# Equipment
id | equipment_set_id | name | gender | class | ...

# Equipment Set
id | name | gender | class

# Equipment Set Bonuses
id  | equipment_set_id | pieces | attribute_id | attribute_value

# Attributes
id | name
Basically, each piece of equipment can belong to one set (or none). Each set has bonuses based on the amount of pieces worn. These bonuses do not have to be cumulative (since sometimes you can get HP+50 from 2/4 pieces and HP+100 from 4/4 pieces). Now, this all makes sense. The problem comes in building the back-end.



I use CodeIgniter as my framework. It's controllers are a series of methods, and all back-end database elements have a controller with CRUD methods. I want to be able to edit the Equipment Set Bonuses through the Equipment Set screen. I cannot technically edit Equipment Set Bonuses without first defining an Equipment Set to add them to (at least without my back-end becoming very unorganized).

Ideally, I'd want to be able to build a table of Equipment Set Bonuses that I can add to, edit, and remove from. But, i set up all of my controllers to be built dynamically. I set the model to use, the form elements to build, and it basically handles itself from there. I'd want to do the same for Equipment Set Bonuses, but only make it accessible when an Equipment Set ID was set (or when a new one is being built).

I don't think I'd want to use AJAX, since the Equipment Set has no ID until after it is created, and I'd want to be able to add the bonuses prior to creating the full Equipment Set. I'm having a tough time doing this elegantly (technically, at all lol).

Any ideas?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Composite controllers?

Post by alex.barylski »

I think your question really threw me off with the subject composite controllers. I'm not sure you would ever want composite controllers, while actions can be chained, the result is not usually composite, but a single filtered result.

The closest thing I can think of would be HMVC, which is a composition of controllers, each action rendering it's own result and then being injected into a master template/layout. This is not the requirement thouggh, from what I have read of your problem. Sounds like you are trying to figure out a way to put the cart before the horse.

If you need records to be created due to associating supplementry records to that record ID, then there is no way around it, the master record must come first. Architecturally, you may be experiencing problems, because you found a way to automate much of the application and now have hit the point of deminishing return, because you cannot solve the issue at hand elegantly using your automated technique. I would personally stick to implementing my controller:actions manually. While it may seem likely or tempting to automate most of what controllers do (because of their simplicity if you follow a strict MVC architecture) there is almost always a case where something like the following occurs and throws off your pattern solution.

All I can say is, MVC triad of components exist for a reason, breaking that pattern, trying to eliminate or automate any of those layers will almost always result in having to return to square one. They are difficult to remove from the picture, without changing the architecural pattern, in fact this is impossible. This is the impression you have given me, with my limited understanding of the problem of course. :)

One hack-ish solution I have used in the past, was to use controller inheritence, although I strongly suggest avoiding this, despite the reduction in controllers:actions you have to implement. Having explicit controller:actions makes your code much easier to comprehend when coming into learning it. Magically loaded and instantiated models and views also confuse people, making those relationships explicitly known (while redundant) makes for easier to read code - unless of course you document this technique and stress over it like there is no tomorrow :)

Anyways, yea, implement a base controller for all the common functionality, and implement an edge case controller to address the unique requirements of this particular problem.

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Composite controllers?

Post by josh »

PCSpectra wrote:One hack-ish solution I have used in the past, was to use controller inheritence, although I strongly suggest avoiding this, despite the reduction in controllers:actions you have to implement.
I half agree. Inheritance in controllers is useful, but now I try not to "pull up" entire actions. Instead, I try to first extract a verbosely named utility method, and pull that up. That being said, I'm not sure what you're asking in this thread? Just bouncing ideas around?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Composite controllers?

Post by Weirdan »

josh wrote:That being said, I'm not sure what you're asking in this thread? Just bouncing ideas around?
I don't think PCSpectra was asking anything... just trying to reply to superdezign's question, albeit in his own verbose manner.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Composite controllers?

Post by josh »

Well I don't understand what super-designs is asking. Should have been more specific. Perhaps I'm not reading into it enough but the only question I saw was "any ideas?" I am trying to clarify if there is a more specific question at hand or if this is a "free for all" thread, so I don't <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> him off... :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Composite controllers?

Post by Christopher »

I may be misunderstanding superdezign's question, but it sounds like the solution is to not use CodeIgniter's CRUD solution. Instead you need to create custom Models and code your own CRUD controller.
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Composite controllers?

Post by superdezign »

Since most of the data for my database exists online, I decided to skip the headache and separate the separate CRUD controllers from each other. Then, I just bult an automation controller to mine data from the other websites.

Maybe one day I'll solve this. :P
Post Reply