Zend Framework 2 Database Registration: Invalid "Secondary" Column Name

I want to save log entries to my MySQL database from Zend Framework 2. I am using Zend\Log\Logger

with writer Zend\Log\Writer\Db

. By providing the author with an array, you can choose which columns to store the data (for example, the timestamp in the "log_date" column) and which data to store. That's what I'm doing:

$logger = new Zend\Log\Logger();

$mapping = array(
    'timestamp' => 'timestamp_column',
    'priority'  => 'priority_column',
    'message'   => 'message_column',
    'extra' => 'extra_column'
);

$logger->addWriter(new Zend\Log\Writer\Db($dbAdapter, 'table_name', $mapping));
$logger->err('some message', array('some extra information'));

      

The problem I'm having is that the array of column names and their values ​​contains the wrong column name for the "extra" column. Based on the above array, it should insert the "extra information" value into the "extra_column" column. The problem is that the class Zend\Log\Writer\Db

uses the letter "e" as the name of the extra column. This comes from the first letter "extra_column" in my array above. For some reason, it takes the first letter "extra_column" and uses that as the column name instead of the entire value.

I looked at the source code. The method is mapEventIntoColumn

used to get the column names and values ​​as an array. I copied the relevant part of the below method.

// Example:
// $event = array('extra' => array(0 => 'some extra information'));
// $columnMap = array('extra' => 'extra_column');
// Return: array('e' => 'some extra information')
// Expected (without looking at the code below): array('extra_column' => 'some extra information')
protected function mapEventIntoColumn(array $event, array $columnMap = null) {
    $data = array();
    foreach ($event as $name => $value) {
        if (is_array($value)) {
            foreach ($value as $key => $subvalue) {
                if (isset($columnMap[$name][$key])) {
                    $data[$columnMap[$name][$key]] = $subvalue;
                }
            }
        }
    }
    return $data;
}

      

The parameter $event

is an array containing the same keys as my array $mapping

in my first code snippet and values ​​for the log message. The parameter $columnMap

is an array $mapping

from my first piece of code (array values ​​are column names).

Actually, it seems that since I am passing the additional information as an array (this is required), the inner foreach loop is executed. There $key

is 0 (the index), so it does the following: $columnMap['extra'][0]

. This gives the letter "e" (the first letter in "extra_column"), which is used as the column name, where the entire column name should be instead.

I tried to provide my own key in an additional array when calling the log method, but the same thing happens. There are no examples in the official documentation of using the additional parameter I want to insert information that can help me debug errors in my table, so I would like to use that.

Is this a bug or am I missing something? It seems strange to me! Hopefully I've explained this well enough - it's pretty tricky!

+3


source to share


1 answer


Since Daniel M hasn't posted his comment as an answer yet, I refer you to his comment, which solved the problem.



+2


source







All Articles