Error converting to vector (-toArray ()) in CakePHP

0

Good evening.

I'm creating a function in the 'src/Model/Table/ProdutosTable.php' file, using version 3 of CakePHP. The function should return information in a vector of a list of products according to the date selected. The problem is that when converting the result of the Query to an array, it becomes empty, not giving continuity to the logic of the code. Could someone help me please?

Below is the code:

src/Model/Table/ProdutosTable.php

//Função para retornar uma lista de produtos.
public function lista($data = null) {
    //Variáveis
    $produtos_model = $this;
    $dados = [];
    $lista_produtos = [];

    //Formata a data.
    $data = new Time($data);
    $data = $data->format('Y-m-d');

    //Seleciona todos os produtos no banco de dados de acordo com os parâmetros passados.
    $query = produtos_model->find()
        ->where(['data' => $data]);

    //Converte o resultado para um array.
    $produtos = $query->toArray(); //Aqui ele deveria realizar a conversão, mas isto não acontece.//

    //Cria a lista com os dados necessarios.
    foreach($produtos as $produto) {
        $dados = [
            'id' => $produto->id,
            'nome' => $produto->nome,
            'referencia' = $produto->referencia
        ];

        array_push($lista_produtos, $dados);
    }

    //Retorna os dados.
    return $lista_produtos;
};
    
asked by anonymous 22.09.2016 / 02:30

2 answers

0

First of all, I would like to thank @DanielOmine again, who are once again helping me here in Stack Overflow. And I would like to apologize, because I inadvertently posted the code with the solution to the problem. That's right, the code that heads this topic already has the bug fix. Again I apologize. When I wrote the code here in Overflow I did not copy it from my development environment and paste it here (since I was out of it). Actually I rewrote the function here, and when I did, I rewrote it with the problem solution without realizing it.

The problem with this question was in the variable ' $data '. This variable is passed from a request ".json" to the controller " ProdutosController.php ". For the sake of organization, I am putting the functions that make access to the database (Except the basic functions of the cake like index (), add (), edit (), delete () ...) in the file quoted at the beginning of the post (% with%).

The issue was that in the file " src/Model/Table/ProdutosTable.php " I already converted that date to an object of type "Time" ( ProdutosController.php ), but did not apply the format function ( $data = new Time($data); ) in it. Then this variable was passed to the list function ( ->format('Y-m-d'); ) with the following content:

object(Cake\I18n\Time) {
    'time' => '2016-09-22T00:00:00+00:00',
    'timezone' => 'UTC',
    'fixedNowTime' => false
},

When should it be passed this way:

2016-09-22

So when I rewrote the function here in Overflow with the excerpt:

//Formata a data.
$data = new Time($data);
$data = $data->format('Y-m-d');

This had already solved the problem;)

Finally, I can not explain why the conversion to a vector ( src/Model/Table/ProdutosTable.php ) was not performed, but when performing the format in the variable ->toArray(); the problem has been remedied. Again I apologize for the fault. And thank you to the whole Overflow community.

    
22.09.2016 / 18:03
0

If you want to get a nested result in a group,

In this section

    $query = produtos_model->find()
        ->where(['data' => $data]);

    //Converte o resultado para um array.
    $produtos = $query->toArray(); //Aqui ele deveria realizar a conversão, mas isto não acontece.//

    //Cria a lista com os dados necessarios.
    foreach($produtos as $produto) {
        $dados = [
            'id' => $produto->id,
            'nome' => $produto->nome,
            'referencia' = $produto->referencia
        ];

        array_push($lista_produtos, $dados);
    }

Modify to

    $query = produtos_model->find('list',
        [
            'keyField' => 'nome',
            'valueField' => 'referencia',
            'groupField' => 'id',
        ]
    )
        ->where(['data' => $data]);

    //Converte o resultado para um array.
    $produtos = $query->toArray();

But it's not clear what the purpose of use is, as there are several options for each purpose.

Maybe instead of list , you'll want to use threaded .

See the examples in the manual link

    
22.09.2016 / 04:30