ZF2-531: Zend log to Db writer and MySQL
Description
The error message I get: Object of class DateTime could not be converted to string in /Zend/Db/Adapter/Driver/Pdo/Statement.php on line 256
The MySQL table I have:
CREATE TABLE logs (
date datetime DEFAULT NULL,
type tinyint default NULL,
event varchar(255) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The code:
<?php
use Zend\Log; use Zend\Db\Adapter\Adapter as DbAdapter;
$showDebug = true; $logDebug = true;
$logger = new Log\Logger;
// add DB writer $dbConfig = array( 'driver' => 'Pdo', 'dsn' => 'mysql:dbname=logs;hostname=localhost', 'username' => 'root', 'password' => 'password', 'driver_options' => array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ), ); $mapping = array( 'timestamp' => 'date', 'priority' => 'type', 'message' => 'event' ); $db = new DbAdapter($dbConfig); $dbWriter = new Log\Writer\Db($db, 'logs', $mapping); $logPriority = $logDebug ? Log\Logger::DEBUG : Log\Logger::INFO; $dbWriter->addFilter(new Log\Filter\Priority($logPriority)); $logger->addWriter($dbWriter);
// do the logging $logger->crit('Crit message goes here!');
Comments
Posted by Fred (topgear) on 2012-09-09T13:20:35.000+0000
Apologies guys. Still had ZF2 beta2 on this desktop. Updated to 2.0.0 and don’t have this problem any more. ZF2 is just great!
Posted by Fred (topgear) on 2012-09-09T13:26:29.000+0000
Was not a bug. I was using the wrong Zend Framework version (beta2) on this desktop.