如何不仅加载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());填充属性,但字段仍然为空。我需要它们,因为我使用它来连接并在DB中创建一个日期字段。怎么把它们装上?
发布于 2016-11-29 13:28:51
应该将迭代器添加到视图中的传递数组中。
<?=$form->field($child, "[$i]birthdate_month", [...])->dropDownList([
'1' => 'January',
...
])?>发布于 2016-11-28 15:49:47
您可以使用datepicker小部件来放置日期、生日、日期、月份和年份,这个小部件是日历。
发布于 2016-11-29 06:21:14
尝尝这个
$formDetails = Yii::$app->request->post('Person', []);
foreach ($formDetails as $i => $value)
{
$model = new Person();
$model->setAttributes($value);
$models[] = $model;
}https://stackoverflow.com/questions/40848142
复制相似问题