Jonathan Cunza
Jonathan Cunza

Reputation: 35

extracting a query result in cakephp 3

Hello everyone will see I have the following query

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

This returns me such an arrangement

query(array) 
 0(object)
  id_alternativa(null)

and what I want is to get the value returned me to then do a validation, not how to retrieve the value returned by the query me to do something like

if($query == null){
    $result = 1;
}else{
    $result = +1;
}

please help

Upvotes: 0

Views: 1514

Answers (2)

Jonathan Cunza
Jonathan Cunza

Reputation: 35

my fues solution this, change the structure of my query

$query = $this->PreguntasAlternativas->find('all', ['conditions' =>       ['id_pregunta' => $idquestion]]);
    $result = $query->max('id_alternativa');
    if(!$result['id_alternativa']){
        $result['id_alternativa'] = 1;
    } else {
        $result['id_alternativa'] += 1;
    }

Thanks

Upvotes: 0

David Yell
David Yell

Reputation: 11855

Queries are lazily executed, and as such your query will be a Cake\ORM\Query object. You will need to execute the query in order to be able to process the results.

If you add ->toArray() to your query, this will cause it to be executed and converted to an array. This will allow you to work with the results.

Upvotes: 1

Related Questions