This is not a issue but a query
i tried prefilling a dummy field with value of another field in model but $model->synonymsList returned undefined
/**
* @inheritdoc
*/
public $synonymsList = [];
public function attributes()
{
$list = parent::attributes();
$list[] = 'synonymsList';
return $list;
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
foreach ($this->synonymsList as $key => $value) {
$tmp[$key] = array('name'=>$value);
}
$this->synonyms = json_encode($tmp);
return true;
}
else {
return false;
}
}
public function afterFind()
{
parent::afterFind();
$this->synonymsList = $this->getsynonymsList();
}
public function getsynonymsList()
{
$synonymsList = [];
$tmp = json_decode($this->synonyms, true);
foreach ($tmp as $key => $value) {
$synonymsList[] = $value['name'];
}
return $synonymsList;
}
}
i ended up doing the following
/**
* Updates an existing CompanyNames model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$synonyms_array = [];
if ($model->load(Yii::$app->request->post())){
if($model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
else{
$synonyms_array = Yii::$app->request->post('CompanyNames')['synonymsList'];
}
} else {
$synonyms_array = $this->getSynomymsArray($model->synonyms);
return $this->render('create', [
'model' => $model, 'synonyms_array' => $synonyms_array
]);
}
}
/**
* converts json to array.
* @param String(json) $synonyms
* @return array $synonyms_array
*/
public function getSynomymsArray($synonyms){
$tmp = json_decode($synonyms,true);
$synonyms_array = [];
foreach ($tmp as $key => $value) {
$synonyms_array[] = $value['name'];
}
return $synonyms_array;
}
what is the error