Returns the maximum of the specified column values.
public max ( string $q, |
||
$q | string | the column name or expression. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
return | mixed | the maximum of the specified column values. |
/** * Creating new order * User have to give restaurant_id and it should exist in table restaurants * Other attributes is optional, gives status value of Order::STATUS_CREATED * * @throws \yii\db\Exception */ public function actionCreate() { $role = UserRoleDetector::getUserRole(); Yii::$app->response->format = Response::FORMAT_JSON; if ($role != 3 && $role != 4) { echo json_encode(array('error_code' => Codes::$UNAUTHORIZED, 'errors' => StatusCodeMessage::$UNAUTHORIZED), JSON_PRETTY_PRINT); } else { $params = $_REQUEST; //checking restaurant_id if (!isset($params['restaurant_id'])) { echo json_encode(array('status_code' => CODES::$BAD_REQUEST, 'message' => StatusCodeMessage::$BAD_REQUEST), JSON_PRETTY_PRINT); exit; } else { $restaurant = Restaurant::findOne(['restaurant_id' => $params['restaurant_id']]); if ($restaurant == null) { echo json_encode(array('status_code' => CODES::$NOT_FOUND, 'message' => StatusCodeMessage::$NOT_FOUND), JSON_PRETTY_PRINT); exit; } } $query = new Query(); $result = $query->createCommand()->insert('orders', ["user_id" => isset($params['user_id']) ? User::findOne(['user_id' => $params['user_id']]) != null ? $params['user_id'] : null : null, "user_info" => isset($params['user_info']) ? $params['user_info'] : null, "restaurant_id" => $params['restaurant_id'], "order_info" => isset($params['order_info']) ? $params['order_info'] : '', "status" => Order::STATUS_CREATED, "other_users" => ''])->execute(); if ($result == null) { echo json_encode(array('status_code' => Codes::$INERNAL, 'message' => StatusCodeMessage::$INERNAL), JSON_PRETTY_PRINT); exit; } else { $model = $query->from('orders')->select(['order_id', 'user_id', 'user_info', 'restaurant_id', 'order_info'])->where(['order_id' => $query->max('order_id')])->createCommand()->queryAll(); echo json_encode(array('status_code' => Codes::$CREATED, 'message' => StatusCodeMessage::$CREATED, 'data' => $model), JSON_PRETTY_PRINT); } } }
public function actionGenerateDb() { $session = Yii::$app->session; $flash = 'Database generated. 10 000 teachers and 100 000 students.'; $break = false; $query = new Query(); $query->select(['id'])->from('{{%teacher}}'); $teachers_count = $query->count(); $query = new Query(); $query->select(['id'])->from('{{%student}}'); $students_count = $query->count(); for ($i = $teachers_count; $i < 10000; $i++) { $command = Yii::$app->db->createCommand(' INSERT INTO {{%teacher}} (name, phone, gender) VALUES(\'' . self::generateString(rand(6, 10)) . '\', \'' . self::generatePhone() . '\', \'' . self::generateGender() . '\') '); $command->execute(); if ($i - $teachers_count == 5000) { $break = true; break; } $break = true; } if (!$break) { for ($i = $students_count; $i < 100000; $i++) { $command = Yii::$app->db->createCommand(' INSERT INTO {{%student}} (name, email, birth_dt, level) VALUES(\'' . self::generateString(rand(6, 10)) . '\', \'' . self::generateEmail() . '\', \'' . self::generateDate() . '\', \'' . self::generateLevel() . '\') '); $command->execute(); if ($i - $students_count == 30000) { $break = true; break; } } } if (!$break) { $query = new Query(); $query->select(['teacher_id'])->from('{{%teacher_student}}'); $teacher_max = $query->max('teacher_id') + 1; for ($i = $teacher_max; $i <= 10000; $i++) { for ($j = 0; $j < rand(2, 5); $j++) { $command = Yii::$app->db->createCommand(' INSERT INTO {{%teacher_student}} (teacher_id, student_id) VALUES(\'' . $i . '\', \'' . rand(1, 100000) . '\') '); try { $command->execute(); } catch (\yii\db\Exception $e) { null; } } if ($i - $teacher_max == 5000) { $break = true; break; } } } if ($break) { $query->select(['id'])->from('{{%teacher}}'); $teachers = $query->count(); $query->select(['id'])->from('{{%student}}'); $students = $query->count(); $flash = 'Teachers = ' . $teachers . '. Students = ' . $students . '. Please, press "Generate teachers and students" again!'; } $session->setFlash('DbGenerated', $flash); $this->redirect('/'); }