Hello. Faced such a problem - I can not get the minimum value from the database, although there is no maximum difficulty.
I have a winner and a loser in the voting, I define them as follows:

public static function getWinner($poll_id) { $winner = self::find()->where(['poll_id' => $poll_id])->andWhere(max(['count']))->one(); return $winner; } public static function getLosers($poll_id) { $losers = self::find()->where(['poll_id' => $poll_id])->andWhere(min(['count']))->one(); return $losers; } 

Requests are identical, with a difference of only min and max. Then through the console, I call
yii winner/competition :

 public function actionCompetition() { $comp = ACompetition::find()->all(); foreach ($comp as $item) { $endPrepareTimeStamp = strtotime($item->endofprepare); $endPollTimeStamp = strtotime($item->endofpoll); $currentTimeStamp = strtotime(date("Ymd H:i:s")); $winner = AOption::getWinner($item->id); if (($currentTimeStamp > $endPrepareTimeStamp) && ($currentTimeStamp > $endPollTimeStamp)) { Yii::$app->db->createCommand("UPDATE a_competition SET winner = '$winner->user_name' WHERE id='$item->id'")->execute(); } $losers = AOption::getLosers($item->id); if (($currentTimeStamp > $endPrepareTimeStamp) && ($currentTimeStamp > $endPollTimeStamp)) { Yii::$app->db->createCommand("UPDATE a_competition SET losers = '$losers->user_name' WHERE id='$item->id'")->execute(); } } } 

The winner is determined correctly, but the loser is not, it turns out that the winner is the loser http://joxi.ru/Rmz5Q6btz0glrO .
If you make var_dump ($ losers), it displays the winner. Although this query in phpmyadmin returns the correct SELECT min(`count`) FROM a_option WHERE poll_id = 1 value SELECT min(`count`) FROM a_option WHERE poll_id = 1 . I assume that there is something wrong in the query in the model, but then why does max work and min does not? Please tell me what is wrong?

  • What requests generates AR watched? - Yaroslav Molchan
  • @YaroslavMolchan apparently here it is he joxi.ru/E2pLnJBHq95l2Y , because on the 102 line just the getLosers () function is. And displays the wrong values ​​that are needed. - Alexey
  • 2
    And what prevents to do so: $winner = self::find()->where(['poll_id' => $poll_id])->orderBy('count DESC')->one(); ? - MasterAlex
  • @MasterAlex does not interfere :) I did not think about this option. But it worked, only in case of a loser you need ASC. Thank! - Alexey
  • @AlexeyDunin, well, yes, it is logical that for losers to do the reverse sorting :) - MasterAlex

1 answer 1

In where it is not so easy to use min and max, you need to do subqueries here, I don’t know your structure, but you should have something similar:

 public static function getWinner($poll_id) { $subQuery = self::find()->max('count'); $winner = self::find()->where(['poll_id' => $poll_id])->andWhere(['count' => $subQuery])->one(); return $winner; } public static function getLosers($poll_id) { $subQuery = self::find()->min('count'); $losers = self::find()->where(['poll_id' => $poll_id])->andWhere(['count' => $subQuery])->one(); return $losers; } 

Use the min and max methods for the subquery and then use the result in the main query.

  • The AR requests are now normal, joxi.ru/KAg0JnPcZEKzrl , joxi.ru/V2VpvMdT4d8Grv . But there is one "but", only one user is defined, although joxi.ru/DrllMybcRVGQrP is correct. I returned the old request for the winner and shared the actionCompetition (). What would separately determine the winner and loser. Then everything is ok with the winners, but the loser was determined only for the second vote: joxi.ru/v29DywXinZQWmG . I will do, thanks for the tip! - Alexey