Add check to prevent orphaned models
This commit is contained in:
@@ -159,11 +159,24 @@ class ManufacturersController extends AdminController {
|
||||
return Redirect::to('admin/settings/manufacturers')->with('error', Lang::get('admin/manufacturers/message.not_found'));
|
||||
}
|
||||
|
||||
// Delete the manufacturer
|
||||
$manufacturer->delete();
|
||||
if ($manufacturer->has_models() > 0) {
|
||||
|
||||
// Redirect to the manufacturers management page
|
||||
// Redirect to the asset management page
|
||||
return Redirect::to('admin/settings/manufacturers')->with('error', Lang::get('admin/manufacturers/message.assoc_users'));
|
||||
} else {
|
||||
|
||||
// Delete the manufacturer
|
||||
$manufacturer->delete();
|
||||
|
||||
// Redirect to the manufacturers management page
|
||||
return Redirect::to('admin/settings/manufacturers')->with('success', Lang::get('admin/manufacturers/message.delete.success'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
return array(
|
||||
|
||||
'does_not_exist' => 'Manufacturer does not exist.',
|
||||
'assoc_users' => 'This manufacturer is currently associated with at least one model and cannot be deleted. Please update your models to no longer reference this manufacturer and try again. ',
|
||||
|
||||
'create' => array(
|
||||
'error' => 'Manufacturer was not created, please try again.',
|
||||
|
||||
@@ -6,4 +6,9 @@ class Manufacturer extends Elegant {
|
||||
'name' => 'required|min:2',
|
||||
);
|
||||
|
||||
public function has_models()
|
||||
{
|
||||
return $this->hasMany('Model', 'manufacturer_id')->count();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user