Domanda

I have the following code in my controller to insert records.

//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>";
            }

The array being looped at, has the following contents (Note that the last record has the field order slightly switched, but as this is an associative array, i thought it should not matter)

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)

This is the output from my getDataSource()->getLog statement

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

You can clearly see that it has only tried to insert the first 2 records, but for some reason it hasn't inserted the 3rd record. Based on the log, it also didn't generate the 3rd Insert SQL statement?

By the time it hits the 3rd loop, the "Save" function fails, this causes $success = false in the end.

Can anyone guess why this is happening?

È stato utile?

Soluzione

My guess is the data in the third entry isn't passing validation.

Possibly your model has the postcode field as numeric only? In that case this data will fail:

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

To debug this, I'd add this line inside your existing failure routine:

var_dump($this->BillingCenterDetail->invalidFields());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top