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

时间:2013-02-12 01:27:30

标签: php mysql zend-framework2

我有以下代码部分,我正在努力从Paypal交易中写入MySQL数据库。我只包含了相关的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);

当我运行我的代码时,打印返回以下内容,以便数据可用

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 ) )

我的表定义如下,我已授予用户完全权限。

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$$

但是,当我在MySQL方面运行跟踪时,它只是不断尝试插入空值。

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

欢迎任何想法!!

2 个答案:

答案 0 :(得分:2)

感谢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']);

我现在看到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  

答案 1 :(得分:1)

尝试替换此行:

$logger->info($mapping);

使用:

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

Zend / Logger的info方法希望收到两个单独的参数。

修改

进一步查看代码,我发现另一个问题。列图只是一种原型。您真正想要记录的是您的数据和一些日志消息,对吗?

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

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

然后:

    $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