Yii Framework Forum: Want to get maximal value, but query doesn't work - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Want to get maximal value, but query doesn't work [solved]

#1 User is offline   tklustig 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 267
  • Joined: 20-April 17
  • Location:Hannover

Posted 14 May 2017 - 09:42 AM

I try to program a query in Yii2, which shows me the highest value of the database. Unfortunately, I get error message: "Call to a member function all() on string" How to patch this problem?

<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\Country;

class CountryController extends Controller
{
    public function actionIndex()
    {


 $query = Country::find();        

 $countries = $query->select('population')->max('population')
   ->all();
			
        return $this->render('index', [
            'countries' => $countries,

        ]);
    }
}

?>

0

#2 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 495
  • Joined: 14-July 12
  • Location:Berlin

Posted 14 May 2017 - 10:41 AM

You have to decide what you want.

"all" gives you all records that match the query, i.e. an array of ActiveRecords.

"max" gives you the maximum value of one column, just one scalar value.
Freelance developer for hire - send me a message!
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users