public function delete($id) { if ($this->shouldLockIfNoPermission('data.edit')) { return; } $customerService = new Customer($this->db); if ($this->request->is('post')) { $deviceService = new Device($this->db); $devices = $deviceService->findAllByCustomerId($id); if (null === $devices) { $this->flash->error('Unable to delete customer!'); return $this->redirect('/customers'); } $customerService->deleteById($id); $partService = new Part($this->db); foreach ($devices as $device) { $partService->deleteAllByDeviceId($device['id']); } $deviceService->deleteAllByCustomerId($id); $this->flash->success('Deleted customer successfully!'); return $this->redirect('/customers'); } $item = $customerService->findById($id); $this->set('item', $item); $this->set('id', $id); }