Populate sub model

I’m drawing a blank. probably easy, but I’m just not seeing it today.

I have the following

Projects => Elements (1-many) => (1-1) Rates

So a project can have multiple Elements, and each Element has a Rate.

I’m trying to add the Rates to the Projects controller, it a new addition.

Typically, we do

            return $this->renderAjax('update', [
                'model' => $model,
                'modelsElements' => (empty($modelsElements)) ? [new ProjectsElements] : $modelsElements,

just not sure how to add the Rates to this equation.

before the return I’m already iterating through the modelsElements for another reason and thought this would be the place to do so, but am drawing a complete blank.

foreach ($modelsElements as $modelElement ) {
    $modelElementRates = $modelElements ->Rates;  // drawing a blank

maybe this is completely wrong.

So I be doing this in the view since I already have the $modelsElements? But of course I need this to get saved with the main model.

Anyone able to kick me in the right direction or provide a link, example to learn from.

As always, thank you for your assistance!

$modelElementRates = $modelElement ->Rates;

But how do I pass that to the renderAjax so it can be processed/saved by retrieving it via the $model->load(Yii::$app->request->post())?

Your suggestion works to edit individual entries within the $modelsElements loop, but not to pass all of them to the views.

This is what I’m having a hard time figuring out

$model = $this->findModel($id);
$modelsElements = $model->projectsElements;
$modelsElementsRates = ????

if ($model->load(Yii::$app->request->post())) {
	// Look at this after

	// foreach ($modelsElements as $modelElement ) {
	// 	$modelElementRates = $modelElement->Rates; //This does work, but only for the current modelElement
	// }

	return $this->renderAjax('update', [
		'model' => $model,
		'modelsElements' => (empty($modelsElements)) ? [new ProjectsElements] : $modelsElements,
		'modelsElementsRates' => ???,

I also figure that I could simply define the model in the view, and I can indeed retrieve the data that way to render it, but upon submission it doesn’t appear to come through with the POST.

something like the following within each Element

 $modelElementRates = $modelElement->elementRates;

 echo $form->field(
                    $modelElementRates ,
                        'template' => '<div class=\"\">{input}</div>' . "\n" . '<div class=\"\">{hint}{error}</div>',
                        'clientOptions' => [
                            'alias' => 'decimal',
                            'digits' => 2,
                            'digitsOptional' => false,
                            'radixPoint' => '.',
                            'groupSeparator' => ',',
                            'autoGroup' => true,
                            'removeMaskOnSubmit' => true,
                            'positionCaretOnTab' => false, //required to enable jQuery select()
                        'options' => [
                            'class' => 'form-control',
                            'finance' => 'true',
                            'tabIndex' => '-1',

but the value never seem to come through to the controller this way once submitted.

I think I finally have the solution. I’ll post back for the benefit of others once I have it all working properly.