简体   繁体   English

Zend Framework 2记录到MySQL DB插入null的其他列

[英]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. 我下面的代码部分正在努力从Paypal事务写入MySQL数据库。 I've only included the relevant Zend DB log section, the Paypal interface is working fine, as per the data output. 我只包含了相关的Zend DB日志部分,根据数据输出,Paypal界面运行良好。

$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. 但是,当我在MySQL方面运行跟踪时,它只会继续尝试插入空值。

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 感谢cathulhu的投入,非常感谢,我在下面完成了这项工作

  // 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 我现在从MySQL一般日志中看到此输出

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. Zend / Logger的info方法期望接收两个单独的参数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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