Check using max function in cakephp 3

0

Hello everyone, as you will see I have this query in sql

SELECT MAX(id_alternativa) FROM pregunta_alternativa WHERE id_pregunta = 7

which I'm doing in cakephp using the max function of the framework

public function register() {
    $query = $this->PreguntasAlternativas->find()
            ->select([
                'id_alternativa' => $query->func()->max(['id_alternativa'])
            ])->where(['id_pregunta' => '7']);

    $this->set('alternatives',$query);
}

but I get an error this is the message

  

Error: Call to a member function func () on null File   C: \ xampp \ htdocs \ serapp \ src \ Controller \ AlternativesController.php   Line: 14

as I see the documentation so its functions are used

SQL functions Cakephp

I do not know what is wrong doing thanks for your help

    
asked by Jonathan Cunza 11.08.2016 в 17:47
source

1 answer

1

You are trying to access the variable $query without defining it previously.

From the Cakephp 3 documentation, you should do it like this:

$query = $this->PreguntasAlternativas->find();
$query->select([
            'id_alternativa' => $query->func()->max(['id_alternativa'])
        ])->where(['id_pregunta' => '7']);

Although you can use chained methods, in this specific case, to use max you MUST first set the variable and then use.

    
answered by 11.08.2016 / 19:25
source