I have a 3 tables/models with the relationship illustrated below:
Account
|
----------
| |
User Company
They are all joined by the the Account’s PRIMARY KEY, and the User and Company each have very different sets of methods and fields.
Assuming that I only know the “account id”, and I need to load an account, I am assuming that the following is a good procedure?
- the Account model is loaded by id
- the “account type” is then determined
-
either a User or a Company object is loaded into the Account object itself. It could be used like so:
$account->company->company_name();
Somehow this doesn’t seem very efficient…can somebody suggest something better?
Probably best to have both User and Company extend Account, and with the parent class Account not knowing about the children classes (only the child classes should know about the parent class) so when you need to add more Account subclasses, you don’t need to change anything in Account: