D
D
denop2018-07-12 02:22:42
Yii
denop, 2018-07-12 02:22:42

Yii2 - category output + form (three tables)?

Hello, there are three tables:
works_prices (id, user_id, work_name_id, price) - link to "works_names"
works_names (id, work_cat_id, name) - link to "works_cat"
works_cat (id, name)
I want to display a table form:
- category jobs
-- job name - [price]
-- job name - [price]
...
- job category
-- job name - [price]
...
Make links and eager loading clear, how best to group?
For starters, I did this:

$works = WorksPrices::find()
            ->joinWith('workName.workCat')
            ->where(['user_id' => $id])
            ->all();

 foreach ($works as $i => $work) {
echo "<div class='col-md-10'>". $work->workName->name ."</div><div class='col-md-2'>";
echo form->field($work, '[' . $i . ']work_price')->textInput()->label(false) . "</div>";
}

Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-07-12
@kimono

$workCats = WorkCat::find()->with(['workNames' => function(ActiveQuery $q){
  return $q->with(['workPrices']);
}])->all();

foreach ($workCats as $workCat){
  echo '<h2>'.$workCat->name.'</h2>';
  foreach ($workCat->workNames as $workName) {
    foreach ($workName->workPrices as $workPrice) {
      echo '<p>'.$workName->name.' ['.$workPrice->amount.']</p>';
    }
  }
}

PS: I did not see it right away, for the form you can:
echo form->field($model, '['.$workCat->id.']['.$workName->id.'][]')->textInput();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question