A
A
AlexAll2018-11-20 19:12:33
Yii
AlexAll, 2018-11-20 19:12:33

How to make a multidimensional array from different table data in yii2 and display it in one variable?

I use this widget https://github.com/unclead/yii2-multiple-input to add links with a title for a post.
So, in his use case, he shows how to use, stuff all the title and link data (in my case) into json and save it in one base field

$model->sites = \yii\helpers\Json::encode($model->sites);
$model->save();

and in order for these fields to be displayed in post editing, use
public function afterFind() {
    parent::afterFind();
    $this->sites = \yii\helpers\Json::decode($this->sites);
         
}

here is the code in the view
<?= $form->field($model, 'sites')->widget(MultipleInput::className(), [
     
    'columns' => [
        [
            'name'  => 'title',
            'title' => 'Название лендинга',
            'enableError' => true,
            'options' => [
                'class' => 'input-priority'
            ]
        ],
        
        [
            'name'  => 'link',
            'title' => 'Ссылка',
            'enableError' => true,
            'options' => [
                'class' => 'input-priority'
            ]
        ]
    ] 
       
 ]);
?>

But the fact is that I don’t need it to be saved in json in one field, I need this data to be saved in a separate table so that it is easy to search for it, I coped with this task, made a table sites (id, title, link, post_id) and I save all this data there.
The problem before me is in the output of these fields in editing. As I understand it, this data (id, title, link, post_id) must be collected into a multidimensional array and assigned to the $model->sites variable.
How to do this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question