我已经创建了注册表。
我有3个选择框。日月年。
在我的数据库中,有一列名为“生日”的格式为“日期”。
现在,我想将这三个部分放在一起并将结果插入数据库中。

public function beforeSave($options = array()){
    $this->data['User']['Birthday'] = $this->data['User']['Year'] . '-' . $this->data['User']['Month'] .'-' . $this->data['User']['Day'];
    return true;
}

但这不起作用:(

该字段(天,月,年,生日)都没有验证规则。

我怎样才能做到这一点?

最佳答案

您可以在模型中尝试以下代码:

public function beforeSave($options = array()){
   if(!empty($this->data)){
       //pr($this->data);die;
       $this->data['User']['Birthday'] = date('Y-m-d', strtotime($this->data['User']['Year'] . '-' . $this->data['User']['Month'] .'-' . $this->data['User']['Day']));
      unset($this->data['User']['Year']);
      unset($this->data['User']['Month']);
      unset($this->data['User']['Day']);
   }
   return true;
}

表单中必须有以下表单字段可用:
<?php echo $this->Form->create('User', array('url' => $this->params));
      echo $this->Form->input('Year', array('options' => $year_options, 'type' => 'select'));
      echo $this->Form->input('Month', array('options' => $month_options, 'type' => 'select'));
      echo $this->Form->input('Day', array('options' => $day_options, 'type' => 'select'));

关于forms - 在模型的beforeSave方法中从表单获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12240889/

10-12 16:50
查看更多