如何在保存/转换为实体之前修改请求数据?
How to modify request data before saving / converting into entities?
在 CakePHP 3.2 中保存数据之前去除 HTML 标记的建议方法是什么?
我正在使用 $this->request->data
构建 newEntity(数据来自表单),稍后我将使用保存功能。
您可以使用 Model.beforeMarshal
event/callback 在将数据转换为实体之前修改数据。
来自文档:
If you need to modify request data before it is converted into
entities, you can use the Model.beforeMarshal event. This event lets
you manipulate the request data just before entities are created:
// In a table or behavior class
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
if (isset($data['username'])) {
$data['username'] = mb_strtolower($data['username']);
}
}
[...]
Cookbook > Database Access & ORM > Saving Data > Modifying Request Data Before Building Entities
在相关说明中,不要忘记不要依赖输入清理,在using/outputting数据时始终确保将其视为可能不安全!
在 CakePHP 3.2 中保存数据之前去除 HTML 标记的建议方法是什么?
我正在使用 $this->request->data
构建 newEntity(数据来自表单),稍后我将使用保存功能。
您可以使用 Model.beforeMarshal
event/callback 在将数据转换为实体之前修改数据。
来自文档:
If you need to modify request data before it is converted into entities, you can use the Model.beforeMarshal event. This event lets you manipulate the request data just before entities are created:
// In a table or behavior class public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) { if (isset($data['username'])) { $data['username'] = mb_strtolower($data['username']); } }
[...]
Cookbook > Database Access & ORM > Saving Data > Modifying Request Data Before Building Entities
在相关说明中,不要忘记不要依赖输入清理,在using/outputting数据时始终确保将其视为可能不安全!