Reputation: 1228
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
Upvotes: 0
Views: 445
Reputation: 8079
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
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 using App::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 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
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.
Upvotes: 2
Reputation: 2623
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...
Upvotes: 2