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