PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Yii2 db getStats (db queries number)

 February 16, 2022     php, yii, yii2     No comments   

Issue

There is useful method getStats in Db-component Yii

$sql_stats = YII::app()->db->getStats();
echo $sql_stats[0] //the number of SQL statements executed
echo $sql_stats[1] //total time spent

Official documentation link

Is there method in Yii2 to get this information?


Solution

Here is equivalent for Yii 2:

$profiling = Yii::getLogger()->getDbProfiling();

$profiling[0] contains total count of DB queries, $profiling[1] - total execution time.

Note that if you want to get information about all queries at the end of request you should execute this code in right place, for example in afterAction():

public function afterAction($action, $result)
{
    $result = parent::afterAction($action, $result);

    $profiling = Yii::getLogger()->getDbProfiling();

    ...

    return $result;
}

Otherwise you will get the information according to the moment of execution this command.

Official documentation:

  • getDbProfiling()
  • afterAction()


Answered By - arogachev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing