Welcome Guest, Not a member yet? Register   Sign In
How to load entrie in a array from a file??
#1

[eluser]altrano[/eluser]
Hello,

I just modify CI 1.7.2 to use with Doctrine ORM.

but now the profiler quries shows not informations about queries because CI->db not used.
I write a hook that uses Doctrine_Connection_Profiler and outputs the queries in a file named doctrine_profiler.php in system/logs folder.
Code:
Array
(
    [0] => Array
        (
            [type] => execute
            [query] => SELECT f.id AS f__id, f.title AS f__title, f.description AS f__description, f.category_id AS f__category_id FROM forum f WHERE (f.id = ?) LIMIT 1
            [time] => 0.000101
            [params] => Array
                (
                    [0] => 1
                )

        )

    [1] => Array
        (
            [type] => execute
            [query] => SELECT DISTINCT t2.id, MAX(p3.created_at) AS p3__1 FROM thread t2 LEFT JOIN post p3 ON t2.id = p3.thread_id LEFT JOIN post p4 ON t2.first_post_id = p4.id LEFT JOIN user u2 ON p4.user_id = u2.id WHERE t2.forum_id = ? GROUP BY t2.id ORDER BY p3__1 DESC LIMIT 4
            [time] => 0.000093
            [params] => Array
                (
                    [0] => 1
                )

        )

    [2] => Array
        (
            [type] => execute
            [query] => SELECT t.id AS t__id, t.title AS t__title, p.id AS p__id, p2.id AS p2__id, p2.created_at AS p2__created_at, u.id AS u__id, u.username AS u__username, (COUNT(p.id) - 1) AS t__0, MAX(p.created_at) AS p__1 FROM thread t LEFT JOIN post p ON t.id = p.thread_id LEFT JOIN post p2 ON t.first_post_id = p2.id LEFT JOIN user u ON p2.user_id = u.id WHERE t.id IN ('1', '2') AND (t.forum_id = ?) GROUP BY t.id ORDER BY p__1 DESC
            [time] => 0.000065
            [params] => Array
                (
                    [0] => 1
                )

        )

    [3] => Array
        (
            [type] => execute
            [query] => SELECT COUNT(*) AS t__0 FROM thread t WHERE (t.forum_id = ?)
            [time] => 0.000057
            [params] => Array
                (
                    [0] => 1
                )

        )

)

Total Doctrine time: 0.00086188316345215
Peak Memory: 4287112

how must i modify the _compile_queries to use this data?
#2

[eluser]WanWizard[/eluser]
This should do the trick
Code:
function _compile_queries()
{
    // don't know doctrine, assume now that your array is called $queries
    
    // Load the text helper so we can highlight the SQL
    $this->CI->load->helper('text');

    // Key words we want bolded
    $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');

    $output  = "\n\n";
        
    $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
    $output .= "\n";
    $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.'DATABASE NAME HERE'.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($queries).'&nbsp;&nbsp;&nbsp;</legend>';
    $output .= "\n";        
    $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
    
    if (count($queries) == 0)
    {
        $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
    }
    else
    {                
        foreach ($queries as $query)
        {                    
            $time = number_format($query['time'], 4);

            $val = highlight_code($query['query'], ENT_QUOTES);

            foreach ($highlight as $bold)
            {
                $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);    
            }
            
            $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$query['query']."</td></tr>\n";
        }
    }
    
    $output .= "</table>\n";
    $output .= "</fieldset>";

    return $output;
}
#3

[eluser]altrano[/eluser]
Thanks i will try it right now Smile
#4

[eluser]altrano[/eluser]
Unfortunately it does not, do not know how to get to the data as an array.

this is my hook:
Code:
class Doctrine_Profiler_Hooks
{
    public static $profiler;

    public function profiler_start()
    {
        self::$profiler = new Doctrine_Connection_Profiler();
        foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
            $conn->setListener(self::$profiler);
        }
    }

    public function profiler_end()
    {
        /* analyze the profiler data */
        $time = 0;
        $events = array();
        foreach (self::$profiler as $event) {
            $time += $event->getElapsedSecs();
            if ($event->getName() == 'query' || $event->getName() == 'execute') {
                $event_details = array(
                    "type" => $event->getName(),
                    "query" => $event->getQuery(),
                    "time" => sprintf("%f", $event->getElapsedSecs())
                );
                if (count($event->getParams())) {
                    $event_details["params"] = $event->getParams();
                }
                $events [] = $event_details;
            }
        }

        $output = "<"."?php  if (!defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
        $output .= print_r($events, 1);
        $output .= "\nTotal Doctrine time: " . $time . "\n";
        $output .= "Peak Memory: " . memory_get_peak_usage() . "";

        file_put_contents(BASEPATH . "/logs/doctrine_profiler.php", $output);
    }
}

it works perfect but i find no solution to pass this to the profiler. Im a stupid boy :-(




Theme © iAndrew 2016 - Forum software by © MyBB