Query yii2 mysql

0

I am trying to make a query in yii2, I am new and I have not yet become familiar with the query topic, the query is as follows:

I need to bring this data:

->select('ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado', 'SUM("ingreso.cantidad*precio_cantidad") AS total')

This is what I tried but it did not work, and try several types of join:

$table = Ingreso::find()
                    ->leftJoin('persona', 'ingreso.idingreso = persona.idpersona')
                    ->leftJoin('detalle_ingreso', 'ingreso.idingreso = detalle_ingreso.idingreso')
                    ->select('ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado', 'SUM("ingreso.cantidad*precio_cantidad") AS total')
                    ->Where(["estado" => 'A'])                                         
                    ->orderBy(['ingreso.idingreso' => SORT_DESC])
                    ->groupBy(['ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado']);
            $pages = new Pagination([
                                "pageSize" => 4,
                                "totalCount" => $table->count()
                                ]);
            $model = $table
                    ->limit($pages->limit)
                    ->all(); 

My tables are these:

Apart from that I throw a mistake with the page in this part:

"totalCount" => $table->count()

I appreciate your help.

    
asked by John Vanegas 22.10.2017 в 22:13
source

2 answers

0

From my point of view the first house that I see wrong is that you are making a query of data type "Revenue" so the answer of your query will be a type of collection of objects type "income", but as you can see in your query you are entering different tables as "person" or even you do SUM operations, if you need to do something like that I do not recommend you use the Model :: find ().

With something appreciated this should be more useful and I think it would be the right way

$query = new Query;
// compose the query
$query->->leftJoin('persona', 'ingreso.idingreso = persona.idpersona')
                ->leftJoin('detalle_ingreso', 'ingreso.idingreso = detalle_ingreso.idingreso')
                ->select('ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado', 'SUM("ingreso.cantidad*precio_cantidad") AS total')
                ->Where(["estado" => 'A'])                                         
                ->orderBy(['ingreso.idingreso' => SORT_DESC])
                ->groupBy(['ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado']);
// build and execute the query
$rows = $query->all();

For the page, I recommend you read the official docummetacion about it, here is a small example

$query = Article::find()->where(['status' => 1]); // puedes substituir esta lina por l o anteriormente explicado 
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
        ->limit($pages->limit)
        ->all();




foreach ($models as $model) {
    // display $model here
}

// display pagination
echo LinkPager::widget([
    'pagination' => $pages,
]);

Paging documentation Yii2: link

    
answered by 14.03.2018 / 17:32
source
-1

The instruction find () - > ... is still missing with the command ...-> all (); or ... - > one ()

Example:

$table = Ingreso::find()
                ->leftJoin('persona', 'ingreso.idingreso = persona.idpersona')
                ->leftJoin('detalle_ingreso', 'ingreso.idingreso = detalle_ingreso.idingreso')
                ->select('ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado', 'SUM("ingreso.cantidad*precio_cantidad") AS total')
                ->Where(["estado" => 'A'])                                         
                ->orderBy(['ingreso.idingreso' => SORT_DESC])
                ->groupBy(['ingreso.idingreso', 'ingreso.fecha_hora', 'persona.nombre', 'ingreso.tipo_comprobante', 'ingreso.serie_comprobante', 'ingreso.num_comprobante', 'ingreso.impuesto', 'ingreso.estado'])
                ->all();
    
answered by 04.12.2017 в 05:07