本文整理汇总了PHP中yii\db\Query::max方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::max方法的具体用法?PHP Query::max怎么用?PHP Query::max使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\db\Query
的用法示例。
在下文中一共展示了Query::max方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionCreate
/**
* 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);
}
}
}
示例2: actionGenerateDb
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('/');
}