gpt4 book ai didi

php - 如何在 Yii 中为单个日期属性设置多个字段(D/M/Y)?

转载 作者:行者123 更新时间:2023-12-04 04:45:07 25 4
gpt4 key购买 nike

我想将用户出生日期放入我的数据库中,并且表中有一个名为 的字段。 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'); ?>

    关于php - 如何在 Yii 中为单个日期属性设置多个字段(D/M/Y)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328125/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com