Advertisement
pmtpenza

Untitled

Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. public function actionCreateiko()
  2. {
  3. $model = new Zayvki;
  4. $orders = [new Order];
  5.  
  6. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  7.  
  8. $orders = Model::createMultiple(House::classname());
  9. Model::loadMultiple($orders, Yii::$app->request->post());
  10.  
  11. // validate person and houses models
  12. $valid = $zayvki->validate();
  13. $valid = Model::validateMultiple($orders) && $valid;
  14.  
  15. if (isset($_POST['Order'][0][0])) {
  16. foreach ($_POST['Room'] as $indexHouse => $rooms) {
  17. foreach ($rooms as $indexRoom => $room) {
  18. $data['Room'] = $room;
  19. $modelRoom = new Room;
  20. $modelRoom->load($data);
  21. $modelsRoom[$indexHouse][$indexRoom] = $modelRoom;
  22. $valid = $modelRoom->validate();
  23. }
  24. }
  25. }
  26.  
  27. if ($valid) {
  28. $transaction = Yii::$app->db->beginTransaction();
  29. try {
  30. if ($flag = $modelPerson->save(false)) {
  31. foreach ($modelsHouse as $indexHouse => $modelHouse) {
  32.  
  33. if ($flag === false) {
  34. break;
  35. }
  36.  
  37. $modelHouse->person_id = $modelPerson->id;
  38.  
  39. if (!($flag = $modelHouse->save(false))) {
  40. break;
  41. }
  42.  
  43. if (isset($modelsRoom[$indexHouse]) && is_array($modelsRoom[$indexHouse])) {
  44. foreach ($modelsRoom[$indexHouse] as $indexRoom => $modelRoom) {
  45. $modelRoom->house_id = $modelHouse->id;
  46. if (!($flag = $modelRoom->save(false))) {
  47. break;
  48. }
  49. }
  50. }
  51. }
  52. }
  53.  
  54. if ($flag) {
  55. $transaction->commit();
  56. return $this->redirect(['view', 'id' => $zayvki->id]);
  57. } else {
  58. $transaction->rollBack();
  59. }
  60. } catch (Exception $e) {
  61. $transaction->rollBack();
  62. }
  63. }
  64. }
  65.  
  66. return $this->render('createiko', [
  67. 'model' => $model,
  68. 'orders' => (empty($orders)) ? [new Order] : $orders,
  69. ]);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement