9 Replies - 535 Views - Last Post: 11 July 2013 - 02:13 AM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Save Data From Multi Dimensional Array/Retrieve Id's

Posted 09 July 2013 - 11:48 AM

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've tried

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
 [CoverageInsured] => Array
        (
            [policy_number] => XMFMSP0015
            [name] => Knowledge Services
            [address_1] =>  Broadway Street  Thermopolis, WY 78542
            [address_2] => 
            [city] => Thermopolis
            [state_id] => 51
            [postal_code] => 82443
            [policy_id] => 
            [coverage_id] => 
        )

 [Declination] => Array
        (
            [0] => Array
                (
                    [policy_number] => DGUEP00071
                    [company] => Casualty  Company
                    [dated] => 1899-12-31
                    [contact_type_id] => 1
                    [first_name] => George 
                    [last_name] => Smith
                    [phone_number] => 254-825-2724
                    [reason_id] => 4
                    [other] => Discountinued
                    [diligent_search] => 
                    [comments] => 
                    [policy_id] => 
                )

[Endorsement] => Array
        (
            [0] => Array
                (
                    [policy_number] => DGUEP00068
                    [endorsement_number] => 
                    [effective_date] => 03/01/2013
                    [endorsement_effective_date] => 03/01/2013
                    [end_date] => 06/01/2055
                    [premium] => 32.24
                    [premium_limit] => 520000
                    [policy_id] => 
                    [producing_id] => 1
                    [filing_id] => 1
                )

[Policy] => 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
                )

[Location] => Array
        (
            [0] => Array
                (
                    [policy_number] => DGUEP00068
                    [address_1] => Merchants Dr.  Hoover, AL 76667
                    [city] =>  Hoover
                    [county] => 
                    [state_id] => 1
                    [postal_code] => 35244
                    [value] => 520000
                    [policy_id] => 
                )
   

This post has been edited by BarNunBoi: 10 July 2013 - 07:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Save Data From Multi Dimensional Array/Retrieve Id's

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,026
  • Joined: 08-June 10

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 09 July 2013 - 11:36 PM

where does your array fit into the given code?
Was This Post Helpful? 0
  • +
  • -

#3 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 06:02 AM

The array is populated from a uploaded spreadsheet. I need to save the Policy info from the spreadsheet so I can retrieve the ids for those policies. Once that is done I need to insert those id's into a table called declinations. Im just having problems with my logic.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,026
  • Joined: 08-June 10

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 06:25 AM

which variable is your array?
Was This Post Helpful? 0
  • +
  • -

#5 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 06:32 AM

My array is called $pulledData
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,026
  • Joined: 08-June 10

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 06:52 AM

that doesn’t make sense. in your code you access array keys that do not exist in this array (and hence would emit Error Notices).
Was This Post Helpful? 0
  • +
  • -

#7 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 07:03 AM

$pulledData is my array and my code works fine to a certain extent. The only error I get is the SQL 1452 error which is telling me that the policy id is not being saved to the declinations table. Could you please elaborate on which part of my code doesn't make sense to you.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,026
  • Joined: 08-June 10

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 07:27 AM

have you looked up what the SQL 1452 error means?
Was This Post Helpful? 0
  • +
  • -

#9 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 10 July 2013 - 07:31 AM

Of course I know what it means.

My foreign key for my declinations table is missing. Which is the policy id I am talking about.

I just updated my array. You should now be able to see everything that is in $pulledData.

This post has been edited by BarNunBoi: 10 July 2013 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,026
  • Joined: 08-June 10

Re: Save Data From Multi Dimensional Array/Retrieve Id's

Posted 11 July 2013 - 02:13 AM

did you regard that you have a 3-level array?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1