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 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序