简体   繁体   中英

Zend Framework 2 logging to MySQL DB additional columns inserting null

I have the following section of code I'm struggling with to get to write to a MySQL database from a Paypal transaction. I've only included the relevant Zend DB log section, the Paypal interface is working fine, as per the data output.

$paypalLog = array(
'transactionId' => 'transactionId',
'transactionType'  => 'transactionType',
'paymentType'   => 'paymentType',
'orderTime' => 'orderTime',
'amt' => 'amt',
'currencyCode' => 'currencyCode',
'feeAmt' => 'feeAmt',
'taxAmt' => 'taxAmt',
'paymentStatus' => 'paymentStatus',
'pendingReason' => 'pendingReason',
'reasonCode' => 'reasonCode'
);    

$data = array(
        'transactionId' => $transactionId,
        'transactionType'  => $transactionType,
        'paymentType'   => $paymentType,
        'orderTime' => $orderTime,
        'amt' => $amt,
        'currencyCode' => $currencyCode,
        'feeAmt' => $feeAmt,
        'taxAmt' => $taxAmt,
        'paymentStatus' => $paymentStatus,
        'pendingReason' => $pendingReason,
        'reasonCode' => $reasonCode
        );

     $mapping = array(
        'message'   => 'message',
        'extra' => $data);

     print_r($mapping);

     $writer = new Zend\Log\Writer\Db($db, 'paypal', $paypalLog);
     $logger = new Zend\Log\Logger();
     $logger->addWriter($writer);
     $logger->info($mapping);

When I run my code the print returns the following so the data is available

Array ( [message] => message [extra] => Array ( [transactionId] => 03V084280U905161H [transactionType] => expresscheckout [paymentType] => instant [orderTime] => 2013-02-12T01:16:40Z [amt] => 9.00 [currencyCode] => AUD [feeAmt] => 0.52 [taxAmt] => 0.00 [paymentStatus] => Completed [pendingReason] => None [reasonCode] => None ) )

My table is defined as the following, and I've granted full permissions to user.

CREATE TABLE `paypal` (
  `timestamp` varchar(45) DEFAULT NULL,
  `priority` varchar(45) DEFAULT NULL,
  `priorityName` varchar(45) DEFAULT NULL,
  `message` varchar(45) DEFAULT NULL,
  `transactionId` varchar(45) NOT NULL,
  `transactionType` varchar(45) DEFAULT NULL,
  `paymentType` varchar(45) DEFAULT NULL,
  `orderTime` datetime DEFAULT NULL,
  `amt` varchar(45) DEFAULT NULL,
  `currencyCode` varchar(45) DEFAULT NULL,
  `feeAmt` varchar(45) DEFAULT NULL,
  `taxAmt` varchar(45) DEFAULT NULL,
  `paymentStatus` varchar(45) DEFAULT NULL,
  `pendingReason` varchar(45) DEFAULT NULL,
  `reasonCode` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`transactionId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$

However when I run a trace on the MySQL side of things it just keeps trying to insert empty values.

130212 12:19:38    29 Connect   user@localhost on database
       29 Query INSERT INTO `paypal` () VALUES ()
       29 Quit  

Any ideas welcome !!

Thanks to cathulhu's input, thanks very much, I got this work with the below

  // Array of db columns
  $paypalLog = array('transactionId' => 'transactionId',
    'transactionType'  => 'transactionType',
    'paymentType'   => 'paymentType',
    'orderTime' => 'orderTime',
    'amt' => 'amt',
    'currencyCode' => 'currencyCode',
    'feeAmt' => 'feeAmt',
    'taxAmt' => 'taxAmt',
    'paymentStatus' => 'paymentStatus',
    'pendingReason' => 'pendingReason',
    'reasonCode' => 'reasonCode'
    );  

  // Map data into array matching column names
  $data = array(            
        'transactionId' => $transactionId,
        'transactionType'  => $transactionType,
        'paymentType'   => $paymentType,
        'orderTime' => $orderTime,
        'amt' => $amt,
        'currencyCode' => $currencyCode,
        'feeAmt' => $feeAmt,
        'taxAmt' => $taxAmt,
        'paymentStatus' => $paymentStatus,
        'pendingReason' => $pendingReason,
        'reasonCode' => $reasonCode
        );

     // Array to construct the DB logger table structure with extra columns
    $mapping = array(   
        'timestamp' => 'timestamp',
        'priority'  => 'priority',
        'priorityName'  => 'priorityName',
        'message'   => 'message',
        'extra' => $paypalLog);      

     // Array to log actual data
     $mylog = array(
        'message' => 'some msg',
        'extra' => $data);

     // Configure the write/logger and specify the array members to log
     $writer = new Zend\Log\Writer\Db($db, 'paypal', $mapping);
     $logger = new Zend\Log\Logger();
     $logger->addWriter($writer);
     $logger->info($mylog['message'], $mylog['extra']);

I now see this output from the MySQL general log

Connect user@localhost on database
       54 Query INSERT INTO `paypal` (`timestamp`,`priority`,`priorityName`,`message`,`transactionId`,`transactionType`,`paymentType`,`orderTime`,`amt`,`currencyCode`,`feeAmt`,`taxAmt`,`paymentStatus`,`pendingReason`,`reasonCode`) VALUES ('2013-02-15T07:52:52+01:00','6','INFO','My log message','88L4229675348363C','expresscheckout','instant','2013-02-15T06:52:58Z','9.00','AUD','0.52','0.00','Completed','None','None')
       54 Quit  

Try to replace this line:

$logger->info($mapping);

with:

$logger->info($mapping['message'], $mapping['extra']);

Zend/Logger's info method expects to receive two separate arguments.

Edit:

Taking further look at your code, I see another problem. Column map is only a kind of prototype. What you really want to log is your data, and some log message, right?

    $mapping = array(
        'message'   => 'message',
         'extra' => $paypalLog);      // Not $data !

    $mylog = array(
        'message' => 'My log message',
        'extra' => $data);

and then:

    $writer = new \Zend\Log\Writer\Db($db, 'paypal', $mapping); // Writer needs mapping info, not data :-)
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $logger->info($mylog['message'], $mylog['extra']);    // ...and your log record goes here, to logger instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM