如何在cakephp中处理相关模型添加操作?

如何在cakephp中处理相关模型添加操作?

问题描述:

I have two models, Dealer and DealerReview.
I want to create a form to add DealerReview from the DealersController, View action. What is the recommended way to achieve this in cakephp? Do i:

a) Create a form in DealersController view action but post the data to DealerReviewsController add action? OR

b) Post the add review data directly to DealersController, view action and add the record to the database from there?

我有两个模型, Dealer code>和 DealerReview code>。 我想创建一个表单来从DealersController,View动作中添加DealerReview。 在cakephp中实现此目的的推荐方法是什么? 我是: p>

a)在DealersController视图操作中创建一个表单,但是将数据发布到 DealerReviewsController code>添加操作? 或者 p>

b)将添加评论数据直接发布到 DealersController code>,查看操作并从那里将记录添加到数据库中? p> div>

Really up to you...

What you have to have in mind when choosing one way or the other is:

for a)
- the dealer that the review is related to will already be created? If not, how will you add the foreign key dealer_id to DealerReviews to relate both?

for b)
- this will nullify the trouble of not having a proper foreign key if the dealer is not created (of course, if you add the creation of dealer first)
- you cannot reuse the add DealerReviews logic if you plan to add reviews from somewhere else.

What I normally do is just post the data directly to the main controller (in this case Dealers). Saves me the trouble of having to check if the Dealer was correctly saved and validated before adding the Review.

If there's a business logic to have in mind before saving the Review (like it can only be save between 2am and 3am (stupid example, I know)), then I add that validation in beforeSave.

If it is a one-or-two-in-a-time business logic, I create a new function saveButOnlyInSomeCasesLogic($reviewArray) in the model and let that handle those cases, the controller just calls that function and waits for the result.

In summary: choose the option that let you reuse code if you need to. If you will always have the foreign key available, do it in the Reviews controller, you may need to reuse the add action. If you will absolutely not reuse the add action or don't have the foreign key available when saving, to it in Dealers, keeping saving logic in the model if any.

Use your association to save this. You can say that a dealer has many dealerReviews. So in this case I would just call save from your dealersController view.

$this->Dealer->DealerReview->save($this->request->data);

This way your form can be in the dealersController like you are wanting.

I would add, depends from the specific use case.

Here is why:

If the review form contain more than one field (but even if you have single textarea for the review, you need to validate at least if the user submit an empty review). If so, on error, you need to return the same form (prepopulated with the posted data) and eventually on the same page. So posting the form in the same action will act as normal add or edit form.

Alternativelly, if you are using an ajax form submit, then I would use the DealersController:add() method and the response the call will be rendered in the place of the original form, but this time with validation errors. It require a little bit tweaking, since you need to detect if the call is from ajax or not, so you don't redirect the add action to the index. The biggest benefit here is that user don't need to refresh the page.

So, if you don't use ajax, I would say - post in the DealersController::view(), but if you are using ajax, then it will be more useful to use DealersReviewsController::add() and you are reusing the method as well as user doesn't have to refresh the page.

As @Nunser said - it's up to you. :)