简体   繁体   English

在cakePHP中修改Form-> input()数据

[英]modifying Form->input() data in cakePHP

A database table for Event model has following fields: 事件模型的数据库表具有以下字段:

user_id, name, title user_id,名称,标题

in the Event add view the user is asked to insert the name, hours and minutes as following: 在事件添加视图中,要求用户输入名称,小时和分钟,如下所示:

    echo $this->Form->input('hours');
    echo $this->Form->input('minutes');
    echo $this->Form->input('name');

Now the name would be obviously stored as it should, but the problem occurs when I want to concacinate the hours and minutes inserted by user and store them into the DBs "title" field. 现在,该名称显然可以按原样存储了,但是当我想弄清用户插入的小时和分钟并将它们存储到DB的“标题”字段中时,就会出现问题。

Any suggestions for how to achieve this? 关于如何实现这一目标的任何建议?

use the model's beforeSave function to add any fields you like. 使用模型的beforeSave函数添加所需的任何字段。 In this case a title is being injected 在这种情况下,标题将被注入

in Event.php 在Event.php中

public function beforeSave($options=array()){
  parent::beforeSave($options);
  $this->data['Event']['title'] = $this->data['Event']['hours'].$this->data['Event']['minutes'];
  //do other stuff
  return true;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM