Using Beforedelete()

hi

my tables is

  • table [work_shifts]{

    id

    title

}

  • table [work_shift_times]{

    id

    work_shift_id

    title

}

  • table [work_shift_overtimes]{

    id

    work_shift_time_id

    title

}

when i delete row in "work_shift" this make error because realtion this table with another table

my model

########work_shifts####

public function relations() {


	return array(


		'workShiftTimes' => array(self::HAS_MANY, 'WorkShiftTimes', 'work_shift_id'),


		 'workShiftOvertimes' => array(self::HAS_MANY, 'WorkShiftOvertimes', array('id'=>'workShiftTimesid'), 'through'=>'workShiftTimes'),


	);


}


protected function beforeDelete()


{


	WorkShiftTimes::model()->deleteAll('work_shift_id = '.$this->id);


	return parent::beforeDelete();


}

########## WorkShiftTimes ######

public function relations() {


	return array(


		'workShiftOvertimes' => array(self::HAS_MANY, 'WorkShiftOvertimes', 'work_shift_time_id'),


		'workShift' => array(self::BELONGS_TO, 'WorkShifts', 'work_shift_id'),


	);


}


protected function beforeDelete()


{





	WorkShiftOvertimes::model()->deleteAll('work_shift_time_id = 14');


	return parent::beforeDelete();


}

##########WorkShiftOvertimes###########

public function relations() {


	return array(


		'workShiftTime' => array(self::BELONGS_TO, 'WorkShiftTimes', 'work_shift_time_id'),


	);


}

##############

this error is view

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 (erpp/work_shift_overtimes, CONSTRAINT fk_work_shift_overtimes_work_shift_times1 FOREIGN KEY (work_shift_time_id) REFERENCES work_shift_times (id) ON DELETE NO ACTION ON UPDATE NO ACT). The SQL statement executed was: DELETE FROM work_shift_times WHERE work_shift_id = 28 (/opt/lampp/htdocs/erp/framework/db/CDbCommand.php:354)

use [code] tags to write your code in the forum. If you paste all the code in regular text like you did it is hard to read and there is lees change you get a reply




my model 

########work_shifts####

public function relations() {

return array(

'workShiftTimes' => array(self::HAS_MANY, 'WorkShiftTimes', 'work_shift_id'),

'workShiftOvertimes' => array(self::HAS_MANY, 'WorkShiftOvertimes', array('id'=>'workShiftTimesid'), 'through'=>'workShiftTimes'),

);

}

protected function beforeDelete()

{

WorkShiftTimes::model()->deleteAll('work_shift_id = '.$this->id);

return parent::beforeDelete();

}

########## WorkShiftTimes ######

public function relations() {

return array(

'workShiftOvertimes' => array(self::HAS_MANY, 'WorkShiftOvertimes', 'work_shift_time_id'),

'workShift' => array(self::BELONGS_TO, 'WorkShifts', 'work_shift_id'),

);

}

protected function beforeDelete()

{


WorkShiftOvertimes::model()->deleteAll('work_shift_time_id = 14');

return parent::beforeDelete();

}

##########WorkShiftOvertimes###########

public function relations() {

return array(

'workShiftTime' => array(self::BELONGS_TO, 'WorkShiftTimes', 'work_shift_time_id'),

);

}



Your problem is in the database, not your application code. You’re attempting to delete a record from work_shift_times which is referenced by at least one record in work_shift_overtimes. Your foreign key is configured so that the deletion is prevented in this instance.

You either need to change the order in which you delete from the tables, so that those which have references to others are deleted first, or you need to alter your foreign key so that deletions are allowed or cause a cascade.