Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionCreateiko()
- {
- $model = new Zayvki;
- $orders = [new Order];
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- $orders = Model::createMultiple(House::classname());
- Model::loadMultiple($orders, Yii::$app->request->post());
- // validate person and houses models
- $valid = $zayvki->validate();
- $valid = Model::validateMultiple($orders) && $valid;
- if (isset($_POST['Order'][0][0])) {
- foreach ($_POST['Room'] as $indexHouse => $rooms) {
- foreach ($rooms as $indexRoom => $room) {
- $data['Room'] = $room;
- $modelRoom = new Room;
- $modelRoom->load($data);
- $modelsRoom[$indexHouse][$indexRoom] = $modelRoom;
- $valid = $modelRoom->validate();
- }
- }
- }
- if ($valid) {
- $transaction = Yii::$app->db->beginTransaction();
- try {
- if ($flag = $modelPerson->save(false)) {
- foreach ($modelsHouse as $indexHouse => $modelHouse) {
- if ($flag === false) {
- break;
- }
- $modelHouse->person_id = $modelPerson->id;
- if (!($flag = $modelHouse->save(false))) {
- break;
- }
- if (isset($modelsRoom[$indexHouse]) && is_array($modelsRoom[$indexHouse])) {
- foreach ($modelsRoom[$indexHouse] as $indexRoom => $modelRoom) {
- $modelRoom->house_id = $modelHouse->id;
- if (!($flag = $modelRoom->save(false))) {
- break;
- }
- }
- }
- }
- }
- if ($flag) {
- $transaction->commit();
- return $this->redirect(['view', 'id' => $zayvki->id]);
- } else {
- $transaction->rollBack();
- }
- } catch (Exception $e) {
- $transaction->rollBack();
- }
- }
- }
- return $this->render('createiko', [
- 'model' => $model,
- 'orders' => (empty($orders)) ? [new Order] : $orders,
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement