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

后端 未结 1 621
生来不讨喜
生来不讨喜 2021-01-22 11:51

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 d

相关标签:
1条回答
  • 2021-01-22 12:23

    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'); ?>
    
    0 讨论(0)
提交回复
热议问题