首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2。型号::loadMultipe。如何也加载类字段?

Yii2。型号::loadMultipe。如何也加载类字段?
EN

Stack Overflow用户
提问于 2016-11-28 15:38:57
回答 3查看 465关注 0票数 0

如何不仅加载DB属性,而且用loadMultipe加载字段?

我有这样一个模特

代码语言:javascript
复制
class Person extends ActiveRecord
{

    public $birthdate_month;

    public $birthdate_day;

    public $birthdate_year;

...

    public function rules()
    {
        return [
            ...
            [['birthdate_month', 'birthdate_day', 'birthdate_year'], 'integer'],
            [['birthdate_month', 'birthdate_day', 'birthdate_year'], 'required']
        ];
    }

// I store data as one DATA field in DB and than use such methods to show it to the user

    public function afterFind()
    {
        if (isset($this->birthdate)) {
            $date = Carbon::createFromFormat('Y-m-d', $this->birthdate); //just a data-manipulation library

            $this->birthdate_month = $date->month;
            $this->birthdate_day   = $date->day;
            $this->birthdate_year  = $date->year;
        }

        return parent::afterFind();
    }



    public function beforeValidate()
    {
        $date            = Carbon::create($this->birthdate_year, $this->birthdate_month, $this->birthdate_day); //just a data-manipulation library
        $this->birthdate = $date->toDateString();

        return parent::beforeValidate();
    }
}

这样的观点

代码语言:javascript
复制
<?php $form = ActiveForm::begin() ?>

<?php foreach ($children as $i => $child): ?> //there are multiple persons (children)

...

 <?=$form->field($child, '[]birthdate_month', [...])->dropDownList([
                    '1'  => 'January',
                    ...
                ])?>

...

<?php endforeach; ?>

...

<?=Html::submitButton('Save', ['class' => 'ui primary button big'])?>
...

<?php ActiveForm::end(); ?>

在控制器中

代码语言:javascript
复制
Person::loadMultiple($children, Yii::$app->request->post());

填充属性,但字段仍然为空。我需要它们,因为我使用它来连接并在DB中创建一个日期字段。怎么把它们装上?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-29 13:28:51

应该将迭代器添加到视图中的传递数组中。

代码语言:javascript
复制
 <?=$form->field($child, "[$i]birthdate_month", [...])->dropDownList([
                    '1'  => 'January',
                    ...
                ])?>
票数 0
EN

Stack Overflow用户

发布于 2016-11-28 15:49:47

您可以使用datepicker小部件来放置日期、生日、日期、月份和年份,这个小部件是日历。

票数 0
EN

Stack Overflow用户

发布于 2016-11-29 06:21:14

尝尝这个

代码语言:javascript
复制
$formDetails = Yii::$app->request->post('Person', []);
foreach ($formDetails as $i => $value) 
{
  $model = new Person();
  $model->setAttributes($value);
  $models[] = $model;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40848142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档