0

我有两张桌子,userstokens
每个用户都有一个activated字段,每个令牌都有{id, token, user_id, created}字段。

该应用程序的工作方式是:在创建时,该应用程序将 -

  1. 确保该activated字段为空(以避免对提交的数据进行操作)。
  2. 将在tokens表中创建一个令牌。

更新时,该应用程序将 -

  1. 不创建新令牌。
  2. 不允许对该activated字段进行任何形式的更新。
  3. 检查是否已提交新电子邮件,如果已提交:将创建一个新令牌并将该activated字段设置为 false。

我知道如何通过控制器激活帐户以及如何为此设置路由器。
我需要的主要是模型配置。
例如:我认为令牌创建应该在afterSave方法中完成,那么 - 我如何确定该方法是由更新还是由创建操作调用?

谢谢你的帮助

4

2 回答 2

1

你的问题不清楚。如果您有一个字段的默认值,那么为什么不在数据库中设置它而不是在 aftersave 中做一些事情呢?如果您需要做一些只在某些情况下才应该做的事情,那么在您的模型中编写一个自定义方法来执行您想要在创建或更新时执行的任务。

编辑

因此,如果您的记录有一个 id,那么您就知道它存在于数据库中。因此,要做的简单事情是(以任何方法)检查​​模型是否具有 id 字段并且它不为空。如果它是空的,那么你知道你正在创建一个记录,你可以做 x 任务。如果不是,那么做你的任务。

if(isset($modelData['ModelName']['id']) && !empty($modelData['ModelName']['id'])){
    //This is an update
} else {
    //This is a new record
}
于 2011-09-02T01:28:11.947 回答
1

yossi you can also specify the fields that should be saved from the form though - a whitelist of fields it is ok to save in you $this->save() call. That way you can stop a hacker passing an ID in the request, and you should just set it in the controller yourself then with $this->Token->id = whatever you have, I would personally use saveField ('activated) in conjunction with this (just saves a single field!). Fat models is best if you can but get it working first then refactor it if you have got stuck. Better than wasting lots of time writing perfect first time.

于 2011-09-02T10:14:37.470 回答