thankyou softark , i am indebted to your time.
I however am not correlating something in regards to the way it picks up the selection for the sale_staff.
I am using a dropdown box
<?= $form->field($saleStaff, 'lister_ids')
->dropDownList($staffs,
['prompt' => 'Select Lister']
)
?>
$saleStaff is the model, $staffs is the array built by the function getAvailableStaffs
in order to allow selection for both lister and seller (i will in future use javascript to allow user to add a new selection to take advantage of the hasMany relation - not too sure how that will work yet either
)
Using this, the lister_ids[] and seller_ids[] is not being populated with anything and i can’t quite correlate how the loadStaffs() function is supposed to do this ?
/**
* load the sale's staffs (*2)
*/
public function loadStaffs()
{
$this->lister_ids = [];
$sls = SaleStaff::find()->where([
'sale_id' => $this->sale_id,
'staff_type' => 'lister'
])->all();
foreach($sls as $sl) {
$this->lister_ids[] = $sl->staff_id;
}
$this->seller_ids = [];
$sss = SaleStaff::find()->where([
'sale_id' => $this->sale_id,
'staff_type' => 'seller'
])->all();
foreach($sss as $ss) {
$this->seller_ids[] = $ss->staff_id;
}
}
Isn’t the job of this function to load in the selections from the view ?
If so, why is this doing a find() on SaleStaff since the data is not saved yet ?
the find()->where above will always return null in this case ?
So I am not correlating how the view is tied to the model saleStaffForm in this instance. I feel like the above loadStaffs() function should simply be getting the data from the _form field selected, but not sure what i’m missing ?
Is my controller logic ok ? Its slightly different to your example, but its taken from gii code i’ve done elsewhere. The property and sale models are being saved correctly.
public function actionCreate()
{
$model = new sale();
$property = new property();
$saleStaff = new saleStaffForm();
if ($model->load(Yii::$app->request->post()) && $property->load(Yii::$app->request->post())) {
$property->save();
$model->property_id = $property->id;
$model->save();
$saleStaff->sale_id = $model->id;
$saleStaff->property_id = $property->id;
$saleStaff->saveStaffs();
} else {
$saleStaff->loadStaffs();
$staffs = SaleStaffForm::getAvailableStaffs();
return $this->renderAjax('create', [
'model' => $model,
'property' => $property,
'saleStaff' => $saleStaff,
'staffs' => $staffs,
]);
}
}