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 callyii 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?
$winner = self::find()->where(['poll_id' => $poll_id])->orderBy('count DESC')->one();? - MasterAlex