Answer the question
In order to leave comments, you need to log in
How is it correct to model a strange array =)?
Hello everyone!))
I am doing MultipleInput for the user_id field. I have an array like this
[
'rang_id' => '1'
'discipline_id' => '1'
'nomination_id' => '1'
'category_id' => '1'
'users' => ''
'user_id' => [
0 => '1'
1 => '2'
2 => '3'
]
]
public function actionCreate()
{
$model = new RegEvent([
'number'=> RegEvent::setNumber(),
'event_id' => RegEvent::getEventId()
]);
$model->scenario = $model::SCENARIO_FRONTEND;
if ($model->load(Yii::$app->request->post())) {
if (is_array($model->user_id)){
foreach ($model->user_id as $user_id) {
$model->user_id = $user_id;
$model->rang_id;
$model->save();
}
}
return $this->redirect(['index']);
}
if (Yii::$app->request->isAjax){
return $this->renderAjax('_form', [
'model' => $model,
]);
}
return $this->render('create', [
'model' => $model,
]);
}
Answer the question
In order to leave comments, you need to log in
if (is_array($model->user_id)){
foreach ($model->user_id as $user_id) {
$model->user_id = $user_id;
$model->rang_id;
$model->save();
}
}
$model->rang_id;
, it does not change, in any case it must be taken out of the cycle. You decide what you want to get. If you need to store an array there, then it might be worth using json (doing it in beforeSave and back in afterFind), if with a string, then you need to combine the array into an implode string , maybe you need 1 more table where regEvent_id | user_id 'users' => '',
'users' => [
0 => '1'
1 => '2'
2 => '3'
]
$users
, and you sort it out in a cycle $model->user_id
, as I understand it, there should have been $model->users
or Yii::$app->request->post('users')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question