cakePHP 新手:模块化方法

发布于 2024-10-14 16:26:34 字数 1634 浏览 0 评论 0原文

我正在开发一个类似groupon的系统,当系统已经构建了70%左右时我就进入了这个项目,并且它是使用cakePHP构建的,说实话,我对cakePHP一无所知。我遇到了这样的情况:

一名会员购买了一笔交易,

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
}

管理员确认某个特定用户已支付了他/她所欠的交易付款

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
}

,现在看到这两者有一些共同点,我正在尝试这样做:

一名会员购买了一笔交易

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

管理员确认:

confirm has_paid
process_deal(parameters)

和 process_deal 将是:

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
    }
}

是否可以做这样的事情?我应该把这个 process_deal 方法放在哪里最好,我试图把它放在 app_controller 类中,但它似乎不会更新表,我不知道为什么它不能更新(我正在使用 updateAll方法),非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-10-21 16:26:34

看起来您可以将此函数作为模型类实现到 /models/deal_user.php/models/deal.php 中,这样您就可以共享 process_deal< /code> 跨越有需要的控制器。

当控制器需要它时,只需包含 ModelClass 即可。较胖的模特。

此外,您不应将此函数包含到您的 app_controller 中,因为在所有其他控制器之间共享此方法或实例化 DealUserDeal< 可能没有意义。 /code> 跨所有控制器的模型,有些可能根本不需要它。

如果您在控制器内尝试过 debug($this),您就会知道数组有多么可怕。包含的 Model 越多,它就会越混乱。

更新

(根据个人经验),当..

/app/bootstrap.php

  • 时,将您的代码放入以下文件中当您在应用程序中的各个地方共享代码时,例如debugjson_encode(当 PHP 版本 < 5.2 时)等

/app/controllers/components/*.php

  • 是当您共享代码/控制器之间的逻辑,数据库交互非常少。

/app/models/*.php

  • (胖模型)是当您在几个控制器之间共享功能时,这些控制器在保存到数据库之前操纵数据,例如上面的问题。

/app/libs/*.php

  • 是当您共享不适合 MVC 的外部代码时,例如 TwitterOauth 或其他通用类/包,这不适合将其转换为组件是有意义的,或者太复杂而无法这样做。使用 App::import('Lib',) 将它们导入到 cake 中就足够了,而且很简洁。

/app/views/helpers/*.php

  • 当您共享要渲染的 html 代码时,在将其从数据转换为 html 代码之前需要一些逻辑。

/app/app_controller.php

  • 当您需要对 beforeFilterbeforeRender 等进行一些修改时,控制器之间需要具有通用功能。原因是因为,您的 *_controller 正在扩展 app_controller,如果您想添加共享组件使用, helpersapp_controller 中,希望在所有控制器中都有它们,结果发现每个请求中都有大量控制器。让它成为一种习惯,尝试让您的 app_controller 尽可能精简。在每个控制器中编写更多代码。

/app/app_model.php

  • 同样的想法也适用于 app_controller

/app/plugins/*/

  • 当你包含外部类似蛋糕的框架/功能时,或者如果需要,您可以将某些框架包装到插件中。

/app/webroot/*/

  • 并且永远不要将您的代码放在 webroot 下!不整齐,破坏Cake的结构,不推荐!这是邪恶的。如果代码不适合 Cake,请考虑将其移出 Cake。

我想这就是全部了?再次强调,它们是我基于编码经验的个人经验。如果您认为它们不正确,请发表评论/编辑。

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.

旧人九事 2024-10-21 16:26:34

我同意上面莱昂内尔·陈的回答。

另外,它可能会帮助您阅读 CakePHP 文档。您尝试做的事情看起来应该不难,但是如果您学习框架并在其中工作,而不是仅使用您已经知道的知识来解决它,那么您会更幸运。

处理某个模型的数据库表的任何代码都应该放在该特定模型类中(Lionel 提到的胖模型)。

因此,您可以将 process_deal 方法放入 Deal 模型中。

然后,DealsController 可以像这样访问该方法:

$this->Deal->process_deal().

然后,如果两个模型之间存在关系,例如:DealUser hasMany Deal,您可以从操作中访问该方法在 DealUsersController 中,如下所示:

$this->DealUser->Deal->process_deal();

如果模型之间没有关系,您仍然可以从 DealUsersController 中的操作中调用 process_deal() 方法 像这样:

$this->loadModel('Deal');
$this->Deal->process_deal();

我希望这可以帮助您指明正确的方向,但我仍然建议您花一两个小时仔细阅读上面链接的 CakePHP 文档,因为您可能会找到大多数问题的答案那里...

PS 感谢您采用模块化方法。看到这里总是让人耳目一新...

I'd go with Lionel Chan's answer above.

Also, it would probably help you to read over the CakePHP documentation. What you're trying to do looks like it shouldn't be difficult, but you'll have much more luck if you learn the framework and work within it rather than trying to work around it just using what you already know.

Any code that deals with a certain model's database table should go in that particular model class (the fat models referred to by Lionel).

So you could put the process_deal method in the Deal model.

The DealsController can then access the method like this:

$this->Deal->process_deal().

Then, if there's a relationship between two models, for example: DealUser hasMany Deal, you can access the method from within an action in the DealUsersController like this:

$this->DealUser->Deal->process_deal();

If there isn't a relationship between the models, you could still call the process_deal() method from within an action in the DealUsersController like this:

$this->loadModel('Deal');
$this->Deal->process_deal();

I hope this helps point you in the right direction, but I'd still recommend spending an hour or two perusing the CakePHP documentation linked to above because you're likely to find the answers to most of your questions there...

P.S. Kudos for going for the modular approach. It's always refreshing to see...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文