Hi all,
Context: I’m an experienced (mature) IT ex-management professional now attempting to re-invent myself by learning & upgrading my technical skills by developing a proof of concept business application with Yii
I’m trying to manage MySQL FK constrained delete situation in Yii (CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row:…)
I’m trying to solve this by using a Rules method triggered by a ‘delete’ scenario, which is probably not the best or most elegant but I’m at the stage where I just want something that works!..
I’m maintaining a count of child records(r007_patient_ct) in the parent (r007_project_centre) and in the r007ProjectCentreModel have the following in its Rules:
…array(‘r007_patient_ct’,‘compare’,‘compareValue’=>‘0’,‘operator’=>’!=’,‘on’=>‘delete’,‘message’=>‘Unable to delete Project Centre - it has Patients’)
In the R007ProjectCentreConroller, I’ve tried to set the ‘delete’ scenario but in the ActionDelete function but despite all sorts of attempts with various syntax versions, have failed
public function actionDelete($id) {
if (Yii::app()->getRequest()->getIsPostRequest()) {
//how do I set up a delete scenario to be triggered in the model e.g.I thought ??$this>setScenario('delete');?
$this->loadModel($id, 'R007ProjectCentre')->delete();
if (!Yii::app()->getRequest()->getIsAjaxRequest())
$this->redirect(array('admin'));
} else
throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
}
Sample error: ‘R007ProjectCentreController and its behaviors do not have a method or closure named “delete”.’
The answer is probably simple & obvious but escapes me, so I’m asking for another brain & pair of eyes on it.
Also, I’m not wedded to this solution! - I’d happily change to a simpler, better way as I’ll be needing this type of functionality elsewhere in the app…
Help! ideas / solutions most welcome
Brgds B