Answer the question
In order to leave comments, you need to log in
Yii2 How to properly handle $_POST data inside beforeSave() method?
Tell me, what is the correct way to accept $_POST data for such a case?
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
Authors2products::deleteAll('products_id = :products_id', [':products_id' => $this->id]);
$post = Yii::$app->request->post();
if(isset($post['Products']['authors'])) {
foreach($post['Products']['authors'] as $authors_id) {
$authors2products = new Authors2products();
$authors2products->authors_id = $authors_id;
$authors2products->products_id = $this->id;
$authors2products->save();
}
}
return true;
}
return false;
}
Answer the question
In order to leave comments, you need to log in
In the controller, use $model->load()
If necessary, set a public variable in the model, such as public $authors;
Then in beforeSave use $this->authors
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question