Recently I came across a new requirement.
“Store user specific information in the database using Zend Log component”.
Although Zend has done tremendous job by creating lots of components, however documentation doesn’t have appropriate examples, so beginner and novice face lot of difficulties in using those components in the beginning.
Although I’d used Zend Log component before-for storing log information in text file, however I hadn’t used it for storing log information in the database.
Studying online manual doesn’t help me lot. Although I got basic idea from the manual, however I was unable to map information to my table- table fields were already defined.
After googling for about some time I got the solution, but example hadn’t cover how to map log information to database, instead explained how to map log data to the xml file.
However I got help from that article and wrote my own code.
I think it’ll help you out.
$db=Zend_Registry::get(‘db’);
$columnMapping = array(
‘table_column_1 =>’map1′,
‘ table_column_1′=>’map2′,
‘ table_column_1′=>’map3′,
‘ table_column_1′ => ‘map4′
);
$writer = new Zend_Log_Writer_Db($db, ‘log_actions’, $columnMapping);
$logger = new Zend_Log($writer);
$logger->setEventItem(‘ map1′, ‘ column 1 info’);
$logger->setEventItem(map1, ‘ column 2 info’);
$logger->setEventItem(‘ map1′, ‘ column 3 info’);
$logger->setEventItem(map4′, ‘ column 4 info’);
First we get instance to our $db. I assume that you have made necessary configuration in your boot file and story reference to “db” using Zend_Registry.
Next we define our “columnMapping” array. Here we are mapping our database table columns (table_column_1, table_column_2…..) to the mapping variables- these variables are used in setEventItem() method later in our code.
In the next line we have defined our writer. Zend has different writers for Db, Xml and file-streams.
As we are logging information in database so we are using Zend_Log_Writer_Db. Give database adapter, table name and columnMapping array as arguments.
Now create Zend_Log instance and give it writer instance as parameter.
The final thing is to store information in the database through mapping fields using setEventItem method.
That it.


