如何不仅使用DB来加载数据库属性,还使用来加载字段loadMultipe
?
我有这样的模特
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();
}
}
这样的看法
<?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(); ?>
在控制器中
Person::loadMultiple($children, Yii::$app->request->post());
填充属性,但字段仍为空。我需要它们,因为我用它来连接数据库并在数据库中创建一个日期字段。如何加载它们呢?
应该在视图中将迭代器添加到传递数组
<?=$form->field($child, "[$i]birthdate_month", [...])->dropDownList([
'1' => 'January',
...
])?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句