1) If you mean working with several models of the same type, the error is in this line:
$valid = $modelAgenda->validade() && $valid;
First, it should be $modelAgenda->validate(), the second $modelAgendacontains an array of models, the method validate()can only be called on one model.
Yii2 validateMultiple():
use yii\base\Model;
...
$valid = Model::validateMultiple($modelAgenda) && $valid;
( ).
, id :
$models = YourModel::find()->index('id')->all();
2) , findAll(), ( ). new create findOne() update . , , $firstModel $secondModel, :
$isSuccess = false;
Yii::$app->db->transaction(function () use ($isSuccess) {
$areLoaded = $firstModel->load(Yii::$app->request->post()) && $secondModel->load(Yii::$app->request->post();
$areSaved = $firstModel->save() && $secondModel->save();
$isSuccess = $areLoaded && $areSaved;
});
if ($isSuccess) {
return $this->redirect(['view', 'id' => $model->id]);
}
( ).
transactions , :
return [
'admin' => self::OP_INSERT,
'api' => self::OP_INSERT | self::OP_UPDATE | self::OP_DELETE,
];
:
$firstModel->scenario = 'scenarioForTransaction';
$secondModel->scenario = 'scenarioForTransaction';
$areLoaded = $firstModel->load(Yii::$app->request->post()) && $secondModel->load(Yii::$app->request->post();
$areSaved = $firstModel->save() && $secondModel->save();
if ($areLoaded && $areSaved) {
return $this->redirect(['view', 'id' => $model->id]);
}
.
P.S. / AJAX, .
- .