Simultaneous creation conflict

I have a controller Create action as follows

if ($model->load(Yii::$app->request->post())) {
	\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

	$modelsNotes = Model::createMultipleNotes(ProjectsNotes::class);
	Model::loadMultiple($modelsNotes, Yii::$app->request->post());

	$modelsLegs = Model::createMultipleLegs(ProjectsLegs::class);
	Model::loadMultiple($modelsLegs, Yii::$app->request->post());

	$model->ProjNo = Projects::find()->max('ProjNo') + 1; //Avoid any conflicts

	// validate all models
	$valid = $model->validate();
	if (! $valid) {
		// ...
	}
	$valid = Model::validateMultiple($modelsNotes) && $valid;
	if (! $valid) {
		// ...
	}
	$valid = Model::validateMultiple($modelsLegs) && $valid;
	if (! $valid) {
		// ...
	}

	if ($valid) {
		$transaction = \Yii::$app->db->beginTransaction();
		try {
			if ($flag = $model->save(false)) {
				foreach ($modelsNotes as $modelNote) {
					$modelNote->ProjId = $model->ProjId;
					if (! ($flag = $modelNote->save(false))) {
						$transaction->rollBack();
						return json_encode(['status' => 'Error', 'message' => 'Note not created!']);
						break;
					}
				}

				foreach ($modelsLegs as $modelLeg) {
					$modelLeg->ProjId = $model->ProjId;
					if (! ($flag = $modelLeg->save(false))) {
						$transaction->rollBack();
						return json_encode(['status' => 'Error', 'message' => 'Leg not created!']);
						break;
					}
				}
			}

			if ($flag) {
				$transaction->commit();
				return json_encode([
					'status' => 'Success', 
					'message' => '<i class="glyphicon glyphicon-ok"></i> Project '.$model->ProjNo.' created successfully.',
					]);
			}
		} catch (ErrorException $e) {
			$transaction->rollBack();
			return json_encode(['status' => 'Error', 'message' => '<i class="glyphicon glyphicon-remove"></i> Project not created!']);
		}
	}
}

it works fine, normally. However, if 2 or more submissions occurs almost simultaneously the $model->ProjNo is the same (which isn’t allowed by the table design, unique) and thus generates an error.

What would be the best way to guarantee in the action a sequential, but unique ProjNo?

I thought about saving the $model immediately before continuing with the rest of the dynamicform models, but then that defeats the purpose of the transaction to guarantee an all or nothing commit of the data. And I’m not sure if even that would truly resolve the issue.