cakePHP 新手:模块化方法
我正在开发一个类似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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您可以将此函数作为模型类实现到
/models/deal_user.php
或/models/deal.php
中,这样您就可以共享process_deal< /code> 跨越有需要的控制器。
当控制器需要它时,只需包含 ModelClass 即可。较胖的模特。
此外,您不应将此函数包含到您的
app_controller
中,因为在所有其他控制器之间共享此方法或实例化DealUser
和Deal< 可能没有意义。 /code> 跨所有控制器的模型,有些可能根本不需要它。
如果您在控制器内尝试过 debug($this),您就会知道数组有多么可怕。包含的
Model
越多,它就会越混乱。更新
(根据个人经验),当..
/app/bootstrap.php
debug
、json_encode
(当 PHP 版本 < 5.2 时)等/app/controllers/components/*.php
/app/models/*.php
/app/libs/*.php
TwitterOauth
或其他通用类/包,这不适合将其转换为组件是有意义的,或者太复杂而无法这样做。使用App::import('Lib',)
将它们导入到 cake 中就足够了,而且很简洁。/app/views/helpers/*.php
/app/app_controller.php
beforeFilter
、beforeRender
等进行一些修改时,控制器之间需要具有通用功能。原因是因为,您的*_controller
正在扩展app_controller
,如果您想添加共享组件
,使用,
helpers
在app_controller
中,希望在所有控制器中都有它们,结果发现每个请求中都有大量控制器。让它成为一种习惯,尝试让您的app_controller
尽可能精简。在每个控制器中编写更多代码。/app/app_model.php
app_controller
/app/plugins/*/
/app/webroot/*/
我想这就是全部了?再次强调,它们是我基于编码经验的个人经验。如果您认为它们不正确,请发表评论/编辑。
Looks like you can have this function implemented into
/models/deal_user.php
or/models/deal.php
as model classes, so you can share theprocess_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 theDealUser
andDeal
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 moreModel
you include, the messier it will be.Update
(Base on personal experience), put your code into following files when..
/app/bootstrap.php
debug
,json_encode
(when PHP version < 5.2) etc/app/controllers/components/*.php
/app/models/*.php
/app/libs/*.php
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 usingApp::import('Lib', <name>)
is pretty sufficient and neat./app/views/helpers/*.php
/app/app_controller.php
beforeFilter
,beforeRender
etc, that need to have common functionalities among controllers. The reason is because, your*_controller
is extendingapp_controller
, and if you are tempted and then add sharedcomponents
,uses
,helpers
in theapp_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 yourapp_controller
as slim as possible. Write more codes in each controllers./app/app_model.php
app_controller
/app/plugins/*/
/app/webroot/*/
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.
我同意上面莱昂内尔·陈的回答。
另外,它可能会帮助您阅读 CakePHP 文档。您尝试做的事情看起来应该不难,但是如果您学习框架并在其中工作,而不是仅使用您已经知道的知识来解决它,那么您会更幸运。
处理某个模型的数据库表的任何代码都应该放在该特定模型类中(Lionel 提到的胖模型)。
因此,您可以将
process_deal
方法放入Deal
模型中。然后,
DealsController
可以像这样访问该方法:然后,如果两个模型之间存在关系,例如:
DealUser hasMany Deal
,您可以从操作中访问该方法在DealUsersController
中,如下所示:如果模型之间没有关系,您仍然可以从
DealUsersController 中的操作中调用
像这样: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 theDeal
model.The
DealsController
can then access the method like this:Then, if there's a relationship between two models, for example:
DealUser hasMany Deal
, you can access the method from within an action in theDealUsersController
like this:If there isn't a relationship between the models, you could still call the
process_deal()
method from within an action in theDealUsersController
like this: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...