Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Cakephp Save ID/Associated Models

$
0
0
I am trying to save the ID for a table called Coverages in a table called Spreadsheets. Im using Cakephp 1.3. I have all my associations correct but for some reason I cannot wrap my head around this task, which I know it is probably simple. This is what I have tried.

View:
<?php echo $this->UiForm->input('coverage.id', array('type' => 'hidden')); ?>


As you can see in my view I have created a hidden input field for my ID.

Controller:
public function processSpreadsheet($coverageId = null) {
		if(!empty($this->data)) {
			if(isset($this->data['Coverage']) && !empty($this->data['Coverage'])) {
				if(array_key_exists('Spreadsheet', $this->data['Coverage']) && $this->data['Coverage']['Spreadsheet']['size'] > 0) {		
					$uploadedFile = $this->data['Coverage']['Spreadsheet'];
					$coverageId = $this->Coverage->id;
					$this->set('coverage.id', $coverageId);
					$spreadsheetId = $this->Coverage->saveSpreadsheet($uploadedFile);
					if($spreadsheetId) {
						$this->Session->setFlash(__('Spreadsheet processed and saved successfully.', true));
						$this->redirect(array(
							'action' => 'edit',
							$spreadsheetId
						));
					} else {
						$this->Session->setFlash(__('Spreadsheet processing failed.', true));
						$this->redirect(array(
							'action' => 'add'
						));
					}
				}
			}
		}
				
	}



I have created a var called CoverageId in the controller also I have set the CoverageId as well.

Model:
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->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->Declination->saveAll($pulledData['Declination']);
					$this->savePolicyData($pulledData);
					$this->Spreadsheet->save($this->data);
					move_uploaded_file($uploadedFile['tmp_name'], $filePath); 
					//$this->Policy->Location->saveAll($pulledData['Location']);
					//$this->Policy->Company->saveAll($pulledData['Policy']);
					return $coverageId;
				}
			}
		}
		
		return false;
	}


Last but not least I am saving data to my spreadsheet table.


Im not sure what Im doing wrong or what I am not doing at all. If someone would please explain to me how this is suppose to work. I have read the documentation but some things are still unclear to me. I think I am doing something wrong in my controller and my model. As far as my model goes I think its just a matter of passing the right argument to the save method, and Im not sure what Im doing wrong in my controller. With that said, any input on this issue will be greatly appreciated!! Thanks!!

I forgot to mention..Im getting the following error when I execute my code....SQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails.

Viewing all articles
Browse latest Browse all 51036

Trending Articles