Answer the question
In order to leave comments, you need to log in
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>";
}
Answer the question
In order to leave comments, you need to log in
$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>';
}
}
}
echo form->field($model, '['.$workCat->id.']['.$workName->id.'][]')->textInput();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question