duanju9104 2015-08-27 10:46
浏览 57
已采纳

Laravel 5拆分模型

PHP noob here.

I have an Item model which is responsible for adding items to my cart.. However the items I'm adding are not simple products. Each item type has a complicated formula for generating a customized quote based on the users selections and personal discount, etc.

The simplest way I can think to do this is to have functions for each product within the item model with each unique formula in. However, this will get messy very fast.

What is the best practice for splitting these into separate files and then including those files in the model, just for keeping my code clean and seperate?

So for example I have aircraft, cars, boats as categories, each product within each category has it's own formula but I at least want to keep aircraft formulas separate from boat for ease of reading.

Is it best to create an aircraft model, boats model etc and have them all use the items DB table and then once they are added I can control them all from the Item model?

I may have just answered my own question, but I don't know :D Learning loads but still a noob.

  • 写回答

1条回答 默认 最新

  • dongqian6234 2015-08-27 13:10
    关注

    I would suggest two things:

    Extract logic from models

    I would extract as much of the business logic as possible from the models - to, say, several formula classes (one for each different formula), which you can easily execute wherever you need to.

    If the data you will save differs between each type, you may want to look into...

    Polymorphic relationships

    See the laravel docs on this for specifics.

    You would have a Product model, that has a polymorphic morphTo relationship (called "type", maybe) to several other "product type" models (that all have an inverse morphOne back to the Product).

    Polymorphic relationships were the first thing i thought of while reading your question, but on re-reading it, the data structure for each type might not be different. If that's the case, ignore the polymorphic bit!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)