1 Replies - 795 Views - Last Post: 04 February 2013 - 08:17 AM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

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

Cakephp Save ID/Associated Models

Posted 31 January 2013 - 02:31 PM

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.

This post has been edited by BarNunBoi: 31 January 2013 - 02:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cakephp Save ID/Associated Models

#2 BarNunBoi  Icon User is offline

  • D.I.C Head

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

Re: Cakephp Save ID/Associated Models

Posted 04 February 2013 - 08:17 AM

I solved my problem!!! I added this to my controller....

$this->data['Spreadsheet']['coverage_id'] = $this->Coverage->id;
$this->Coverage->Spreadsheet->save($this->data);


With that said, the reason I was having so much trouble is because CakePHP doesn't have saving multiple relationships/associations documented very well for CakePHP 1.3. I found this post on StackOverFlow that helped me get over the hump! Hope this helps someone!!

This post has been edited by BarNunBoi: 04 February 2013 - 08:20 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1