I think you are doing everything right. You can do something more general.
Each model has certain relationships, makes some (or all) of the relationships deleted. You must mark them (somehow) delete along with the current record.
I would define a new function besides relations() in the model.
function deletableRelations() { return array('ftpuser', 'childs', .....); }
and define some common function (please specify DB transactions in this function.)
function DeleteRelationships($model) { foreach( $model->deletableRelations() as $relation ) { if( $model->$relation == null) continue; if( is_array($model->$relation)) { foreach( $model->$relation as $relRecord ) { $relRecord->delete(); } } else { $model->$relation->delete(); } } }
And this versatile feature can be used by any of your models. Make sure that recursion does not occur here (i.e. the parent removes the child ... but the child also tries to remove the parent.)
source share