Yii Framework Forum: Unable To Update The Model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Unable To Update The Model Rate Topic: -----

#1 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 31 December 2012 - 10:51 AM

Hi Expert friends,

Im using the relational database with relations in my model.

when i'm trying to update my_Model(by uploading new image i.e, by deleting old image) i'm getting the following error. how can i overcome this problem.


CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`imember`.`organisation`, CONSTRAINT `organisation_logo` FOREIGN KEY (`logo_file_id`) REFERENCES `file` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: DELETE FROM `file` WHERE `file`.`id`='21'



my controller updateAction is like this:



public function actionUpdate($id)
	{
		//retrieving
		$fileModel = new File;
		$model=MyModel::model()->findByPk($id);
		$file = File::model()->findByPk($model->file_id);
			
		

		if (isset($_POST['MyModel'])) {
			$model->attributes = $_POST['MyModel'];
			if($fileModel->file = CUploadedFile::getInstance($model, 'file')){
		
				//deleting the old file,dir and record in file table if new file is uploading
				unlink(getcwd().'/uploads/'.$model->file_id.'/'.$file->file_name.$file->extension);
				rmdir(getcwd().'/uploads/'.$model->file_id);
				File::model()->findByPk($model->file_id)->delete();
				$fileModel->attributes = $_POST['MyModel'];
				
				
				   
				if ($model->validate()) {
				
				
				if ($fileModel->saveImage()) {
						
					// update the organisation
					$model->file_id = $fileModel->id;
					if ($model->save())




how can i overcome this problem..

Any help pls

Thanks in advance
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 31 December 2012 - 11:22 AM

I am just going to paste some code which works for me:



		$file=CUploadedFile::getInstanceByName('file');

		if ($file instanceof CUploadedFile) {

			$attributePath=$this->uploadPath;

			if (!in_array(strtolower($file->getExtensionName()),array('gif','png','jpg','jpeg'))) {
				throw new CHttpException(500,CJSON::encode(
					array('error'=>'Invalid file extension '. $file->getExtensionName().'.')
				));
			}

			// $fileName=trim(md5($attribute.time().uniqid(rand(),true))).'.'.$file->getExtensionName();
			$fileName = $file->getName();

			$path=$attributePath.DIRECTORY_SEPARATOR.$fileName;

			if (file_exists($path) || !$file->saveAs($path)) {
				throw new CHttpException(500,CJSON::encode(
					array('error'=>'Could not save file or file exists: "'.$path.'".')
				));
			}

"Less noise - more signal"
0

#3 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 358
  • Joined: 23-July 09

Posted 31 December 2012 - 12:27 PM

The error is very clear:

CONSTRAINT `organisation_logo` FOREIGN KEY (`logo_file_id`) REFERENCES `file` (`id`) 


You cannot delete the record in the table until you delete the corresponding record or NULL `logo_file_id` in the corresponding table.
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#4 User is offline   developer! 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 110
  • Joined: 28-November 12

Posted 02 January 2013 - 09:26 AM

View PostBackslider, on 31 December 2012 - 12:27 PM, said:

The error is very clear:

CONSTRAINT `organisation_logo` FOREIGN KEY (`logo_file_id`) REFERENCES `file` (`id`) 


You cannot delete the record in the table until you delete the corresponding record or NULL `logo_file_id` in the corresponding table.



Yes, I realised it. thanks for your reply. I'm trying to delete parent table record. i have deleted child record first then tried to delete parent record..its working fine now



thanks for your reply all :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users