Tuesday, July 9, 2013

Save Data From Multi Dimensional Array/Retrieve Id's

Im having some trouble getting some data from a multi dimensional array to save correctly. I need data in this array to save so I can retrieve the ids for those records, insert those ids into another table, and finish saving the rest of the array. I am trying to save the Policy ids into a table called Declinations. Policies hasMany Declinations and Declinations belongs to Policy. I just can't seem to get my head around the logic for this. Below is my code and what I have tried so far. Also here is what my array looks like. Please note the array for Policies can grow up to 500.

    public function saveSpreadsheet($uploadedFile) {
if(!empty($uploadedFile) && !empty($uploadedFile['tmp_name'])) {
if($uploadedFile['error'] == UPLOAD_ERR_OK) {
$pulledData = $this->extract($uploadedFile['tmp_name']);
$filePath = WWW_ROOT.'/spreadsheet/' . DS . $uploadedFile['name'] ;
if(!empty($pulledData)){
$pulledData = $this->Policy->Declination->ContactType->setSpreadsheetContactTypeIds($pulledData);
if(!empty($pulledData[$this->alias])) {
$this->save($pulledData[$this->alias]);
unset($pulledData[$this->alias]);
$coverageId = $this->id;
$pulledData = $this->setSpreadsheetCoverageIds($pulledData, $coverageId);
$pulledData = $this->CoverageInsured->setSpreadsheetCoverageInsuredIds($pulledData);
//$pulledData = $this->Policy->Location->setupSpreadsheetPolicyLocation($pulledData, $coverageId);
}
$this->CoverageInsured->save($pulledData['CoverageInsured']);
$this->savePolicyData($pulledData);
$this->Policy->Declination->saveAll($pulledData['Declination']);
move_uploaded_file($uploadedFile['tmp_name'], $filePath); 
//$this->Policy->Location->saveAll($pulledData['Location']);
//$this->Policy->Company->saveAll($pulledData['Policy']);
return $coverageId;
}
}
}
return false;
}



This is what I was trying 

    public function saveSpreadsheet($uploadedFile) {
if(!empty($uploadedFile) && !empty($uploadedFile['tmp_name'])) {
if($uploadedFile['error'] == UPLOAD_ERR_OK) {
$pulledData = $this->extract($uploadedFile['tmp_name']);
$filePath = WWW_ROOT.'/spreadsheet/' . DS . $uploadedFile['name'] ;
if(!empty($pulledData)){
$pulledData = $this->Policy->Declination->ContactType->setSpreadsheetContactTypeIds($pulledData);
if(!empty($pulledData[$this->alias])) {
$this->save($pulledData[$this->alias]);
unset($pulledData[$this->alias]);
                                                $this->save($pulledData['Policy']);
                                                unset($pulledData['Policy']);
                                                $policy_id = $this->Policy->id;
                                                die(debug('$policy_id'));
$coverageId = $this->id;
$pulledData = $this->setSpreadsheetCoverageIds($pulledData, $coverageId);
$pulledData = $this->CoverageInsured->setSpreadsheetCoverageInsuredIds($pulledData);
//$pulledData = $this->Policy->Location->setupSpreadsheetPolicyLocation($pulledData, $coverageId);
}
$this->CoverageInsured->save($pulledData['CoverageInsured']);
$this->savePolicyData($pulledData);
$this->Policy->Declination->saveAll($pulledData['Declination']);
move_uploaded_file($uploadedFile['tmp_name'], $filePath); 
//$this->Policy->Location->saveAll($pulledData['Location']);
//$this->Policy->Company->saveAll($pulledData['Policy']);
return $coverageId;
}
}
}
return false;
}


    Array
(      [0] => Array          (              [policy_number] => DGUEP00068              [policy_type] =>               [policy_fee] =>               [line_of_buisness] => Equity Protection              [premium] => 32.24              [premium_limit] => 520000              [coverage_id] =>               [producing_id] => 1              [filing_id] => 1          )        [1] => Array          (              [policy_number] => DGUEP00058              [policy_type] =>               [policy_fee] =>               [line_of_buisness] => Equity Protection              [premium] => 25694.52              [premium_limit] => 1200000              [coverage_id] =>               [producing_id] => 1              [filing_id] => 1          )


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: