How to have multiple fields (D/M/Y) for single date property in Yii?

核能气质少年 提交于 2019-12-02 03:44:23

问题


I want to take user birth day into my database and there is a field in the table called dob. When I created model and CRUD it generated text field for dob as always. But I want to create three inputs.

  1. For years
  2. For Months
  3. and for dates

So my question is how to add extra inputs in the model's form. I was thinking of adding new attributes to the model class but there are no such attributes in the table.


回答1:


Add the fields to your model:

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;
}

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'); ?>


来源:https://stackoverflow.com/questions/18328125/how-to-have-multiple-fields-d-m-y-for-single-date-property-in-yii

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!