且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Yii中的单个日期属性如何具有多个字段(D/M/Y)?

更新时间:2023-11-09 20:40:52

将字段添加到模型中

public $year;
public $month;
public $date;

将以下方法添加到模型中:

Add these methods to your model:

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表单中使用它们:

You can now use them in your CActiveForm form:

<?php echo $form->textField($model, 'year'); ?>
<?php echo $form->textField($model, 'month'); ?>
<?php echo $form->textField($model, 'date'); ?>