Cakephp - 没有为所有插入的记录生成SQL

时间:2014-03-10 23:52:00

标签: cakephp foreach sql-insert

我的控制器中有以下代码用于插入记录。

//Perform Inserts
            $success = true;                 

            foreach($toinsertrecords as $toinsertrecord) {

                $this->BillingCenterDetail->create();
                if (!$this->BillingCenterDetail->save($toinsertrecord)) {
                    $success = false;
                    echo "insert fail";
                }                    else {echo "insert success";}
            }

            if ($troubleshoot) {
                $log = $this->BillingCenterDetail->getDataSource()->getLog(false, false);
                echo "<pre>Output from Datasource Log";
                var_dump($log);
                echo "</pre>";
            }

循环的数组具有以下内容(请注意,最后一条记录的字段顺序略有切换,但由于这是一个关联数组,我认为这无关紧要)

array (size=3)
  0 => 
    array (size=1)
      'BillingCenterDetail' => 
        array (size=12)
          'startdate' => string '2014-03-10' (length=10)
          'enddate' => string '2014-03-10' (length=10)
          'billing_center_id' => string '50' (length=2)
          'isactive' => boolean false
          'addr1' => string 'melbourne' (length=9)
          'addr2' => string 'melbourne' (length=9)
          'addr3' => string 'melbourne' (length=9)
          'city' => string 'melbourne' (length=9)
          'postcode' => string '777' (length=3)
          'country' => string 'aus' (length=3)
          'email' => string 'a@a.com' (length=7)
          'phone' => string '1234' (length=4)
  1 => 
    array (size=1)
      'BillingCenterDetail' => 
        array (size=12)
          'startdate' => string '2014-03-12' (length=10)
          'enddate' => string '2028-12-10' (length=10)
          'billing_center_id' => string '50' (length=2)
          'isactive' => boolean false
          'addr1' => string 'melbourne' (length=9)
          'addr2' => string 'melbourne' (length=9)
          'addr3' => string 'melbourne' (length=9)
          'city' => string 'melbourne' (length=9)
          'postcode' => string '777' (length=3)
          'country' => string 'aus' (length=3)
          'email' => string 'a@a.com' (length=7)
          'phone' => string '1234' (length=4)
  2 => 
    array (size=1)
      'BillingCenterDetail' => 
        array (size=12)
          'billing_center_id' => string '50' (length=2)
          'startdate' => string '2014-03-11' (length=10)
          'enddate' => string '2014-03-11' (length=10)
          'isactive' => string '1' (length=1)
          'addr1' => string 'test' (length=4)
          'addr2' => string 'test' (length=4)
          'addr3' => string 'test' (length=4)
          'city' => string 'test' (length=4)
          'postcode' => string 'test' (length=4)
          'country' => string 'test' (length=4)
          'email' => string 'test@test' (length=9)
          'phone' => string 'test' (length=4)

这是我的getDataSource() - &gt; getLog语句

的输出
Output from Datasource Log

array (size=3)
  'log' => 
    array (size=4)
      0 => 
        array (size=5)
          'query' => string 'SELECT `BillingCenterDetail`.`id`, `BillingCenterDetail`.`startdate`, `BillingCenterDetail`.`enddate`, `BillingCenterDetail`.`billing_center_id`, `BillingCenterDetail`.`isactive`, `BillingCenterDetail`.`addr1`, `BillingCenterDetail`.`addr2`, `BillingCenterDetail`.`addr3`, `BillingCenterDetail`.`city`, `BillingCenterDetail`.`postcode`, `BillingCenterDetail`.`country`, `BillingCenterDetail`.`email`, `BillingCenterDetail`.`phone`, `BillingCenterDetail`.`created`, `BillingCenterDetail`.`modified` FROM `bm`.`bil'... (length=718)
          'params' => 
            array (size=0)
              ...
          'affected' => int 1
          'numRows' => int 1
          'took' => float 0
      1 => 
        array (size=5)
          'query' => string 'BEGIN' (length=5)
          'params' => 
            array (size=0)
              ...
          'affected' => int 1
          'numRows' => int 1
          'took' => float 0
      2 => 
        array (size=5)
          'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-10', '2014-03-11', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a@a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
          'params' => 
            array (size=0)
              ...
          'affected' => int 1
          'numRows' => int 1
          'took' => float 0
      3 => 
        array (size=5)
          'query' => string 'INSERT INTO `bm`.`billing_center_details` (`startdate`, `enddate`, `billing_center_id`, `isactive`, `addr1`, `addr2`, `addr3`, `city`, `postcode`, `country`, `email`, `phone`, `modified`, `created`) VALUES ('2014-03-13', '2028-12-10', 50, '0', 'melbourne', 'melbourne', 'melbourne', 'melbourne', '777', 'aus', 'a@a.com', '1234', '2014-03-11 15:47:15', '2014-03-11 15:47:15')' (length=374)
          'params' => 
            array (size=0)
              ...
          'affected' => int 1
          'numRows' => int 1
          'took' => float 0
  'count' => int 4
  'time' => float 0

您可以清楚地看到它只尝试插入前2条记录,但由于某种原因它没有插入第3条记录。基于日志,它还没有生成第3个Insert SQL语句?

当它到达第3个循环时,“保存”功能失败,这最终导致$ success = false。

任何人都可以猜到为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我的猜测是第三个条目中的数据没有通过验证。

您的模型可能只将邮政编码字段设为数字?在这种情况下,这些数据将失败:

 'postcode' => string 'test' (length=4)

要对此进行调试,我会在现有的故障例程中添加此行:

var_dump($this->BillingCenterDetail->invalidFields());