CakePHP is a wonderful framework. Recently I proved it to myself once again (not that I need much of that proof anyway). The problem that we had at work was a whole lot of code in once place and no obvious way of how to break that code into smaller, more manageable pieces. MVC is a fine architecture, but it wasn’t obvious to me how to apply it to larger projects.
In our particular case, we happen to have several data types, which are very similar to each other, yet should be treated differently. Two examples are:
- Client account registrations. Our application supports different types of accounts and each type has its own processing, forms, validations, etc. However, each type of account is still an account registration.
- Financial transactions. Our clients can send us money via a number of payment methods – credit cards, PayPal, bank wires, etc. Each type of the transaction has its own processing, forms, validations, etc. However, each type of the transaction is still a financial transaction of money coming in.
Having a separate model for each type of account or for each type of transaction seems excessive. There are differences between each type, but not enough to justify a separate model. Having a single model though means that it’ll continue to grow with each and every difference that needs to be coded in. Something like a class factory design pattern would solve the problem nicely, but the question is how to fit it into an existing MVC architecture. Read the rest of this post for a demonstration.
Continue reading CakePHP : Building factories with models and behaviors