我想将用户出生日期存入我的数据库,表中有一个字段称为dob。当我创建模型和 CRUD 时,它生成了文本字段dob一如既往。但我想创建三个输入。
- 多年
- 几个月来
- 和日期
所以我的问题是如何在模型的表单中添加额外的输入。我正在考虑向模型类添加新属性,但表中没有这样的属性。
将字段添加到您的模型中:
public $year;
public $month;
public $date;
将这些方法添加到您的模型中:
protected function afterFind() {
parent::afterFind();
$dob = explode('/', $this->dob);
$this->year = $dob[0];
$this->month = $dob[1];
$this->date = $dob[2];
return $this;
}
protected function beforeSave() {
parent::beforeSave();
$this->dob = $this->year .'/'. $this->month .'/'. $this->date;
return $this;
}
您现在可以在 CActiveForm 表单中使用它们:
<?php echo $form->textField($model, 'year'); ?>
<?php echo $form->textField($model, 'month'); ?>
<?php echo $form->textField($model, 'date'); ?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)