cakePHP - 创建新用户帐户,几个问题
我有两个表,users
和 tokens
。
每个用户都有一个 activated
字段,每个令牌都有 {id, token, user_id,created}
字段。
该应用程序的工作方式应该是: 创建时,应用程序将 -
- 确保
activated
字段为空(以避免对提交的数据进行操作)。 - 将在
tokens
表中创建一个令牌。
更新时,应用程序将
- 不会创建新令牌。
- 不允许对
activated
字段进行任何类型的更新。 - 检查是否已提交新电子邮件,如果是:将创建一个新令牌并将
activated
字段设置为 false。
我知道如何通过控制器激活帐户以及如何为此设置路由器。
我需要的主要是模型配置。
例如: 我认为令牌创建应该在 afterSave
方法中完成,那么 - 如何确定该方法是由更新调用还是由创建操作调用?
感谢您的帮助
I have two tables, users
and tokens
.
Each user have a activated
field and each token have the {id, token, user_id, created}
fields.
The way the app should work is:
On the creation, the app will -
- make sure that the
activated
field is empty (to avoid manipulations to the submitted data). - a token will be created in the
tokens
table.
On update, the app will -
- NOT create a new token.
- NOT allow an update of any kind to the
activated
field. - check if a new email has been submitted, and if so: will create a new token and set the
activated
field to false.
I know how to activate the account through the controller and how to setup the router for that.
What I need is mainly the model configuration.
For example:
I think that the token creation should be done in the afterSave
method, so - how do I determine if the method is called by an update or by a create operation?
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
yossi 您还可以指定应该从表单中保存的字段 - 可以在 $this->save() 调用中保存的字段白名单。这样你就可以阻止黑客在请求中传递 ID,你应该自己在控制器中设置它,然后使用 $this->Token->id = 无论你有什么,我个人会使用 saveField ('activated)与此结合(仅保存一个字段!)。如果可以的话,胖模型是最好的,但首先让它工作,然后在遇到困难时重构它。比第一次就浪费大量时间写出完美的文章要好。
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.
你的问题不清楚。如果某个字段有默认值,那么为什么不在数据库中设置它而不是在保存后执行某些操作?如果您需要执行仅在某些情况下执行的操作,请在模型中编写自定义方法以在创建或更新时执行您想要的任务。
编辑
因此,如果您的记录有一个 id,那么您就知道它存在于数据库中。因此,要做的简单的事情就是(以任何方法)检查模型是否有 id 字段并且它不为空。如果它是空的,那么您就知道您正在创建一条记录并且可以执行 x 任务。如果不是,则执行任务。
You question is unclear. If you have a default value for a field, then why not set it in the database rather than doing something in aftersave? If you need to do something that should be done only in certain circumstances, then write a custom method in your model to perform the tasks you want either on creation or update.
Edit
So, if your record has an id, then you know it exists in the database. So, the simple thing to do is (in any method) check to see if the model has an id field and that it is not empty. If it's empty, then you know that you are creating a record and you can do x task. If it isn't, then do y task.