PHP中如何处理不同的账户类型?
我有 3 个表/模型,其关系如下所示:
Account
|
----------
| |
User Company
它们全部由帐户的主键连接,并且用户和公司各自具有非常不同的方法和字段集。
假设我只知道“帐户ID”,并且我需要加载一个帐户,我假设以下是一个好的程序?
- 帐户模型通过 id 加载,
- 然后确定“帐户类型”
将用户对象或公司对象加载到帐户对象本身中。它可以像这样使用:
$account->company->company_name();
不知怎的,这似乎不是很有效......有人可以建议更好的东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能最好让用户和公司都扩展帐户,并且父类帐户不知道子类(只有子类应该知道父类),因此当您需要添加更多帐户子类时,不需要更改帐户中的任何内容:
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: