cakephpcakephp-2.0cakephp-1.3cakephp-2.1cakephp-2.3

how to display max and min number from database in cakephp


how to display max and min number in cakephp controller part .

$rs = $this->Item->find('all' , array ('fields' => array('Max(Item.no) as no')));

I want max number of Item table .

I need to add some rand number to that and save it in new row .


Solution

  • Use below query

    $rs = $this->Item->find('all' , array ('fields' => array('Max(Item.no) as no')));
    

    This query will return max number in below format

    Array
    (
        [0] => Array
            (
                [no] => 27
            )
    
    )
    
    $max = $rs[0]['no'];