dongyi2889 2011-01-27 10:04
浏览 61
已采纳

cakePHP上的新手:模块化方法

i am developing a groupon-like system, and i came into this project when the system is already around 70% built, and it was built using cakePHP, to be honest, i know nothing about cakePHP. and i came across this:

a member bought a deal

if(has_enough_account_balance){
    if((parameters validated)){
        insert into 'deal_user' table 
        log transaction
        update 'deal' table by:
            user_count = current user_count + bought deal //to determine whether this deal is tipped or not
        if(this deal is tipped){
            issue coupon
        }
    }
}else{
    this_user_owed
}

the admin confirmed that a particular user has paid his/her owed deal payment

confirm has_paid
update into 'deal_user' table 
log transaction
update 'deal' table by:
    user_count = current user_count + bought deal //to determine whether this deal is tipped or not
if(this deal is tipped){
    issue coupon
}

now seeing that the two of those has something in common, i am trying to do this:

a member bought a deal

if(has_enough_account_balance){
    if((parameters validated)){
        process_deal(parameters)
    }
}else{
    this_user_owed
}

on admin confirmation:

confirm has_paid
process_deal(parameters)

and process_deal would be:

function process_deal(parameters){
    if(isset(deal_id)){
        update into 'deal_user' table 
    }else{
        insert into 'deal_user' table
    }
    log transaction
    update 'deal' table by:
        user_count = current user_count + bought deal //to determine whether this deal is tipped or not
    if(this deal is tipped){
        issue coupon
    }
}

is it possible to do things like this ? and where is the best place should i put this process_deal method, i have tried to put this inside the app_controller class, but it seems that it wont update the table, i am not sure why can't it update (i am using the updateAll method), thank you very much

  • 写回答

2条回答 默认 最新

  • dongwei6700 2011-01-27 14:40
    关注

    Looks like you can have this function implemented into /models/deal_user.php or /models/deal.php as model classes, so you can share the process_deal across needy controllers.

    When a controller need it, simply include the ModelClass. Fatter Models.

    and furthermore, you should not include this function into your app_controller as it might not make sense sharing this method across all other controllers, or instantiating the DealUser and Deal models across all controllers, and some might not need it at all.

    If you tried debug($this) inside a controller, you know how horrible the array is. The more Model you include, the messier it will be.

    Update

    (Base on personal experience), put your code into following files when..

    /app/bootstrap.php

    • Is when you have shared codes among everywhere in your application, like debug, json_encode (when PHP version < 5.2) etc

    /app/controllers/components/*.php

    • Is when you have shared code/logic among controllers, with very minimal DB interaction.

    /app/models/*.php

    • (Fat Models) Is when you have shared functionalities among a few controllers, that manipulate the data before saving into the DB, example such as your question above.

    /app/libs/*.php

    • Is when you have shared external codes that doesn't fit into MVC, like TwitterOauth, or other generic classes/packages, which does not make sense converting it into Components, or too complex to do so. Import them into cake by using App::import('Lib', <name>) is pretty sufficient and neat.

    /app/views/helpers/*.php

    • Is when you have shared html codes to render, that required some logic before converting it from data into html codes.

    /app/app_controller.php

    • Is only when you need to do some hack on beforeFilter, beforeRender etc, that need to have common functionalities among controllers. The reason is because, your *_controller is extending app_controller, and if you are tempted and then add shared components, uses, helpers in the app_controller with the hope of having them in all controllers, it turns out that you have heavy controllers in every requests. Make it a habit try make your app_controller as slim as possible. Write more codes in each controllers.

    /app/app_model.php

    • Same idea goes for app_controller

    /app/plugins/*/

    • Is when you include external cake-like framework/features, or you can wrap certain framework into a plugin if you want.

    /app/webroot/*/

    • And never never put your code under webroot! It's not neat, break Cake's structure, and it's not recommended! It's evil. Considering moving your code out of Cake if they couldn't be fit into Cake.

    I think that is all? Again, they are my personal experience base on coding experience. Do comment/edit if you think they are not correct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄