Search code examples
yii2find-by-sql

How to print findBySql yii2


im using findBySql in yii2, i need to print the count of the query, but i only got the 'cidade' index.

Query :

$a2m = AlarmesCorrentes::findBySql('SELECT COUNT(CIDADE) AS CONTA, CIDADE 
from 
alarmes_correntes WHERE  lastUpdate >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
GROUP  BY 
CIDADE ORDER BY CONTA DESC LIMIT 7 ')->all();

Printing :

  foreach($a2m as $m ){

    echo "<pre>";
    print_r($m['CIDADE']);
   // print_r($m['CONTA']);
    echo "</pre>";
  }

?>

Getting unknown property: app\modulos\dashboard_intragov\models\AlarmesCorrentes::CONTA

My DB data in View Format:

My DB data in view format


Solution

  • the simplest way is in your model AlarmesCorrentes

    add a public var with the same name of the alias you are using eg: $conta

    class AlarmesCorrentes extends \yii\db\ActiveRecord
    {
    
        public $conta;
       .......