php - ZF2 ZendLog + Doctrine2


I do not know how to configure Zend \ Log with Doctrine2. Only allows you to write directly to the database via a connection adapter or write to a file.




May be it's too late to answer this question but better late than never.

I've found a good post which explains how to create a basic for ZF2 and Doctrine.

The approach is pretty simple :

1. Creating {-code-5} class : Create the following class in your{-code-2} folder :


The{-code-4} function which is called by Doctrine when it finiches sending the query to the database server, is overrided so that it could write the current query to the{-code-5} object.

2. Configuring the {-code-5} : Add the following code in your{-code-6} file, to make the {-code-5} accessible to the{-code-7} using the name{-code-8} :

        'service_manager' => array(
            'factories' => array(
                '{-code-8}' => function($sm) {
                    $log = new \{-code-14}\{-code-5}();
                    $writer = new \{-code-14}\Writer\Stream('./{-code-11}');

                    $sqllog = new \Application\Log\Sql{-code-5}($log);
                    return $sqllog;

The{-code-5} will write data to the{-code-11} file. So, make sure that{-code-12} folder exists in your application root directory.

3. Configuring Doctrine : Now you need to tell Doctrine to use the created {-code-5}. Just add the following code to your Doctrine configuration :

    return array(  
        'doctrine' => array(
             /*--------Add this code     



--*/ 'sql_logger_collector' => array( 'orm_default' => array( 'sql_logger' => '{-code-8}', ), ), /*



---*/ 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 'params' => array( 'host' => 'localhost', 'port' => '3306', 'user' => 'username', 'password' => 'password', 'dbname' => 'dbname', ), ), ), ), );

With the above configuration of{-code-14} and{-code-15}, you'll get all the query data logged in the{-code-16} file.

Please see this Sql Logger for ZF2 and Doctrine for more details.



--*/ 'sql_logger_collector' => array( 'orm_default' => array( 'sql_logger' => 'my_sql_logger', ), ), /*


---*/ 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver', 'params' => array( 'host' => 'localhost', 'port' => '3306', 'user' => 'username', 'password' => 'password', 'dbname' => 'dbname', ), ), ), ), );|||Zend\Log|||Doctrine2|||data/log/sql.log

People are also looking for solutions to the problem: javascript - Ripping off HTML tags and considering line breaks in a textarea


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.